PersistenceManager的makePersistent方法无法可靠地工作。同一个junit-test正在工作,过了一段时间它又失败了?! 我有一个包含其他对象集合的对象。这意味着1-n关系。 我的问题是我的类的junit-test有时失败,因为集合中的对象没有正确保存。 我通常使用事务,但persistanceManager是事务可选的
我尝试在每个createObject之后检查objectState并提交... 我发现成功提交后集合中的对象没有systemId(应该是自动生成的),但处于空心/持久 - 非事务状态。
这意味着有时它们(如果它们被持久化):
注释[systemId = Project(1)/ Comment(6),JDO-ObjectState = hollow / persistent-nontransactional]
有时它们是(如果它们没有持久化并且父对象的重新包含空集合) 注释[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
}
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
}
答案 0 :(得分:1)
解决方案始终是为每个事务使用新的persistanceManager。永远不要重复使用pm !!
答案 1 :(得分: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;
}