简体   繁体   English

拥有一对多rs的makePersistent。 在JDO中无法可靠地工作

[英]makePersistent for owned one-to-many rs. not working reliably in JDO

the makePersistent method of the PersistenceManager is not working reliably. PersistenceManager的makePersistent方法无法正常工作。 one and the same junit-test is working and after a while its failing again?! 一个同一个junit-test正在运行,一段时间后再次失败? i have an object that includes a collection of other objects. 我有一个包含其他对象集合的对象。 that means an 1-n relationship. 这意味着1-n关系。 my problem is that the junit-test of my classes is failing sometimes because the objects in the collection are not persisted properly. 我的问题是我的类的junit-test有时会失败,因为集合中的对象没有正确持久化。 i am using usually transactions but the persistanceManager is transaction-optional 我通常使用交易,但persistanceManager是交易可选的

i tried checking the objectState after each createObject and commit... i figured out that the objects in the collection after a successful commit have no systemId (should be auto-generated) but are in state hollow/persistent-nontransactional. 我尝试在每个createObject和commit之后检查objectState…我发现成功提交之后集合中的对象没有systemId(应自动生成),但是处于空心/持久非事务状态。

that means sometimes they are (if they are persisted): 这意味着有时它们是(如果坚持的话):

Comment [systemId=Project(1)/Comment(6), JDO-ObjectState=hollow/persistent-nontransactional] 注释[systemId = Project(1)/ Comment(6),JDO-ObjectState =空心/持久性非事务性]

and sometimes they are (if they are not persisted and a refach of the parent-object contains an empty collection) Comment [systemId=null, JDO-ObjectState=hollow/persistent-nontransactional] 有时它们是(如果未持久化并且对父对象的引用包含一个空集合) 注释[systemId = null,JDO-ObjectState = hollow / persistent-nontransactional]

off course i could check manually if all the stored objects have an systemId but this approach is not nice at all. 当然,我可以手动检查所有存储的对象是否都具有systemId,但是这种方法根本不好。 the commit should just fail!! 提交应该只是失败!

i do not understand that it is sometimes working and sometimes not! 我不明白这有时会奏效,有时却不会奏效! that means my code should not be wrong and this is a bug... pls help me out 这意味着我的代码应该没有错,这是一个错误...请帮帮我

PS: i can post some code if necessary! PS:如有必要,我可以发布一些代码!

@PersistenceCapable
public class Ble implements Serializable, JDOObject<Ble> {

/**
 * 
 */
private static final long serialVersionUID = 1L;

// NotNull
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key systemId;

// NotNull
@Persistent
private Key parentId;

// NotNull
@Persistent
@Extension(vendorName = "datanucleus", key = "gae.parent-pk", value = "true")
private Key projectId;

// NotNull
@Persistent
private String title;

@Persistent
private int position;

@Persistent
private boolean hasChildren;

@Persistent
private BleData requirementData;

@Persistent
private List<Comment> comments;


//getter/setter
}

childObject childObject

    @PersistenceCapable
public class Comment implements Serializable, JDOObject<Comment> {

/**
 * 
 */
private static final long serialVersionUID = 1L;

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key systemId;

@Persistent
private String text;

@Persistent
private long createdTimestamp;

//getter//Setter    
}

the solution is always to use a new persistanceManager for each transaction. 解决方案始终是为每个事务使用新的persistanceManager。 never reuse a pm!! 永远不要重复使用下午!

the Log if i try to add a comment and it's NOT working 日志,如果我尝试添加评论但它不起作用

05.09.2012 15:02:06 org.datanucleus.jdo.metadata.JDOMetaDataManager$MetaDataRegisterClassListener registerClass
INFO: Listener found initialisation for persistable class com.Comment
05.09.2012 15:02:06 org.datanucleus.store.appengine.MetaDataValidator validate
INFO: Performing appengine-specific metadata validation for com.Comment
05.09.2012 15:02:06 org.datanucleus.store.appengine.MetaDataValidator validate
INFO: Finished performing appengine-specific metadata validation for com.Comment
Comment [systemId=null, text=testAddCommentToBle - comment , createdTimestamp=1346850126819, JDO-Status=hollow/persistent-nontransactional]

the Log if i try to add a comment and it IS working 日志,如果我尝试添加评论,它正在工作

05.09.2012 15:00:25 org.datanucleus.jdo.metadata.JDOMetaDataManager$MetaDataRegisterClassListener registerClass
INFO: Listener found initialisation for persistable class com.Comment
05.09.2012 15:00:26 org.datanucleus.store.appengine.MetaDataValidator validate
INFO: Performing appengine-specific metadata validation for com.Comment
05.09.2012 15:00:26 org.datanucleus.store.appengine.MetaDataValidator validate
INFO: Finished performing appengine-specific metadata validation for com.Comment
Comment [systemId=Project(1)/Comment(6), text=testAddCommentToBle - comment , createdTimestamp=1346850025996, JDO-Status=hollow/persistent-nontransactional]

the add method looks like this. add方法如下所示。

public Boolean addCommentToBle(Key systemKey, Comment comment)
        throws Exception {
    PersistenceManagerFactory PMF = PersistenceUtil.getPersistenceManagerFactory();
    PersistenceManager pm = PMF.getPersistenceManager();
    pm.currentTransaction().begin();
    Ble ble= pm.getObjectById(Ble.class, systemKey);
    System.out.println(ble);
    ble.getComments().add(comment);
    pm.makePersistent(ble);
    pm.currentTransaction().commit();
    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM