0

PersistenceManager 的 makePersistent 方法工作不可靠。一个相同的junit-test正在工作,过了一会儿又失败了?!我有一个包含其他对象集合的对象。这意味着 1-n 关系。我的问题是我的类的 junit-test 有时会失败,因为集合中的对象没有正确保存。我通常使用事务,但persistanceManager 是事务可选的

我尝试在每次 createObject 并提交后检查 objectState ...我发现成功提交后集合中的对象没有 systemId(应该自动生成)但处于空心/持久非事务状态。

这意味着有时它们是(如果它们被持久化):

注释 [systemId=Project(1)/Comment(6),JDO-ObjectState=hollow/persistent-nontransactional]

有时它们是(如果它们没有持久化并且父对象的 refach 包含一个空集合) 评论 [systemId=null, JDO-ObjectState=hollow/persistent-nontransactional]

当然,如果所有存储的对象都有一个 systemId,我可以手动检查,但这种方法一点都不好。提交应该失败!

我不明白它有时有效,有时无效!这意味着我的代码不应该是错误的,这是一个错误......请帮助我

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
}

子对象

    @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    
}
4

2 回答 2

1

解决方案是始终为每个事务使用新的 persistenceManager。永远不要重复使用一个下午!

于 2012-12-18T14:59:50.757 回答
0

如果我尝试添加评论并且它不起作用,则日志

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]

如果我尝试添加评论并且它正在工作,则日志

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]

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;
}
于 2012-09-05T12:51:17.787 回答