2017-12-30 7 views
0

새 개체를 개체 속성으로 설정할 때마다 새 개체가 만들어집니다. 누군가가 이것을 재현하도록하십시오. 기본 동작입니까? 설정 1-1 기존 개체와의 관계가 JDO를 사용하는 대신 새 개체를 삽입 하시겠습니까?

public class ProductClass implements Serializable { 
... 
@PrimaryKey 
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 
long id; 
@Persistent 
private String name = null; 
... 
} 


public class Product implements Serializable { 
... 
@PrimaryKey 
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 
long id; 
@Persistent 
private String name = null; 
@Persistent(defaultFetchGroup = "true") 
private ProductClass productClass = null; 
... 
} 

내가 내 ProductClassNeo4jService를 구현하여 데이터베이스에서 ProductClass를 검색하는 방법이다;

@Override 
public ProductClass findById(Long id) { 
    if (id == null) { 
     return null; 
    } 
    PersistenceManager pm 
      = JDOUtil.PERSISTENCE_MANAGER_FACTORY.getPersistenceManager(); 
    ProductClass prod = null; 

    try { 
     prod = (ProductClass) pm.getObjectById(ProductClass.class, id); 
    } finally { 
     pm.close(); 
    } 
    return prod; 
} 

... 
ProductClass prodClass = productClassNeo4jService.findById(id); 

제품을 이와 같이 유지합니다.

... 
Product prod = new Product(); 
prod.setName('productName'); 
prod.setProductClass(prodClass); 

PersistenceManager pm = JDOUtil.PERSISTENCE_MANAGER_FACTORY.getPersistenceManager(); 
Transaction tx = pm.currentTransaction(); 
try { 
tx.begin(); 
pm.makePersistent(prod); 
Object id = pm.getObjectId(prod); 
tx.commit(); 
} finally { 
    if (tx.isActive()) { 
     tx.rollback(); 
    } 
    pm.close(); 
} 
... 

ProductClass를 Product로 설정하는 대신 새로운 ProductClass가 생성됩니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

+0

"문자열이 아닌 개체"란 무엇입니까? 당신은 1-1 관계가 있습니다! 'prodClass' 파일은 어디에 생성됩니까? 'makePersistent'를 호출 할 때 어떤 객체 생명주기 상태입니까? 이 모든 것은 JDO 스펙과 JDO 문서에 정의되어 있습니다. – DN1

+0

@ DN1 감사합니다. 어떻게 prodClass를 검색하는지 추가했습니다. productClass가 이미 생성되었습니다. 새 제품을 생성하려면 제품이 하나의 productClass에 속해야합니다. 당신이 말했듯이 1-1 관계입니다. 권리? – Hopecee

+0

그래서 트랜잭션 외부에서 검색되었으므로 TRANSIENT입니까? 또는 탈퇴 했습니까? – DN1

답변

0

부착/분리이라는 JDO 2.0 덕분에 다른 곳에서 사용하기 위해 객체를 분리 할 수 ​​있으며 객체 내의 변경된 데이터를 유지하려는 경우 부착 할 수 있습니다. 얻은 ProductClass는 그것을 떼어 내지 않고 쓸모가 없었다. @ DN1에 감사드립니다. 선을 추가하여 ProductClass를 분리하도록 코드를 수정했습니다.

이렇게 보입니다.

@Override 
public ProductClass findById(Long id) { 
    if (id == null) { 
     return null; 
    } 
    PersistenceManager pm 
      = JDOUtil.PERSISTENCE_MANAGER_FACTORY.getPersistenceManager(); 
    Transaction tx = pm.currentTransaction(); 

    ProductClass prod = null; 

    try { 
     tx.begin(); 

     prod = (ProductClass) pm.getObjectById(ProductClass.class, id); 
     // Detach our productClass object for use elsewhere 
     prod = pm.detachCopy(prod); 

     tx.commit(); 
    } finally { 
     if (tx.isActive()) { 
      tx.rollback(); 
     } 
     pm.close(); 
    } 
    return prod; 
} 

의 편의를 위해 당신의 PMF 옵션에 javax.jdo.option.DetachAllOnCommit = true을 추가합니다.

실제 문제는 내가이 모든 것을 외부에서 수행하고 있다는 것입니다. 거래. persistence.xmljavax.jdo.option.DetachAllOnCommit을 추가하면 Transaction에 대해서만 작동합니다.

+0

하지만 트랜잭션을 사용하는 경우에는'detachCopy'가 필요하지 않습니다. 트랜잭션없이이 작업을 수행하는 경우'datanucleus.detachOnClose'를 설정하면 PM을 닫을 때 모든 읽기 전용 객체가 분리됩니다. – DN1

+0

@ DN1 예. 나는 그것을 관찰했다. 그것은 왜 시간이 걸렸고, 도움을받지 못하는 이유를 알아내는 데 도움이되었습니다. 감사. – Hopecee

+0

트랜잭션없이 Data를 읽는 것을 선호하기 때문에'persistence.xml'에서'datanucleus.DetachAllOnCommit = true'와'datanucleus.detachOnClose = true' 둘 다 설정하는데 충돌이 있습니까? – Hopecee

관련 문제