2011/1/27 Jan Willies <j...@willies.info> > Hi, > > I can't have my child-object have an order. The following code fails > with: > > javax.jdo.JDOFatalUserException: Detected attempt to establish > Topic(2) as the parent of SubTopic(9) but the entity identified by > SubTopic(9) has already been persisted without a parent. A parent > cannot be established or changed once an object has been persisted. > > > @PersistenceCapable(identityType = IdentityType.APPLICATION, > detachable = "true") > public class Topic { > > @PrimaryKey > @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) > private Key key; > > @Order(extensions = @Extension(vendorName="datanucleus", key="list- > ordering", value="position asc")) > @Persistent(defaultFetchGroup = "true", mappedBy = "topic") > @Element(dependent = "true") > private List<SubTopic> subTopics = new ArrayList<SubTopic>(); > > If I leave out the @Order everything works as expected. I do need the > ordering however. Is there some other way to achive the same? >
Ok, I solved it. What I did wrong (?) was that I added the children to the parent outside a PersistenceManager, ie: Topic topic = topicDao.findById(topicId); SubTopic subTopic = new SubTopic(); topic.getSubTopics().add(subTopic); topicDao.saveOrUpdate(topic); Now I'm doing this: Topic topic = pm.getObjectById(Topic.class, topicId); topic.getSubTopics().add(subTopic); pm.makePersistent(topic); While I was at it, I added transactions. My DAO now looks like this: @Override public void addSubTopic(Long topicId, SubTopic subTopic) { PersistenceManager pm = PMF.get().getPersistenceManager(); Transaction tx = pm.currentTransaction(); tx.begin(); try { Topic topic = pm.getObjectById(Topic.class, topicId); topic.getSubTopics().add(subTopic); pm.makePersistent(topic); tx.commit(); } finally { try { if (tx.isActive()) tx.rollback(); } finally { pm.close(); } } } -- You received this message because you are subscribed to the Google Groups "Google App Engine for Java" group. To post to this group, send email to google-appengine-java@googlegroups.com. To unsubscribe from this group, send email to google-appengine-java+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-appengine-java?hl=en.