What version of OpenJPA are you using? > Seems to me that version check is not happening the way I expect but I am trying to find out how to do that. You mention a version field, but I don't see one in your Entity snippets ... are you missing a version field, or did you omit the field from the posting?
Thanks, Rick On Fri, Oct 22, 2010 at 3:44 PM, ksarum <[email protected]> wrote: > > Experts, > > I want to persist/merge a persistent object tree with parent child > relationships. Here I do replace children of the parent with new instances > but am retaining the same primary key values (I use sequence generated > primary keys). When I merge the updated tree, I expect OpenJPA to update > the > children with existing primary keys. Instead it simply deletes child > records > as they are not the same detached instances but newly created instances. It > just ignores the primary key field. Seems to me that version check is not > happening the way I expect but I am trying to find out how to do that. > > I have to update the whole tree in presentation tier and pass it on to > persistence layer for updating the whole tree in one shot instead of > looping > through each child entities. I cannot depend on the other tier to retain > the > exact detached instance but the data. Severed children should be deleted > and > the modified ones (that new instances retaining primary keys) should be > updated. How to get this work in OpenJPA like saveOrUpdate in hibernate? > Appreciate your help. > > Here is the code for your review > > TestMaster.java > @Entity > @Table(name="TEST_MASTER") > public class TestMaster implements Serializable { > > @Id > @Column(name="ID") > @SequenceGenerator(name="testMasterIdSeq", > sequenceName="TEST_MASTER_SEQ") > @GeneratedValue(generator="testMasterIdSeq", > strategy=GenerationType.SEQUENCE) > private Integer id; > > @Column(name="FIELD") > private String field; > > @OneToMany(mappedBy="master", cascade=CascadeType.ALL, > fetch=FetchType.EAGER) > @ElementDependent(true) > private Set<TestChild> children; > > //public getter/setters > ----------------------------------- > TestChild .java > > @Entity > @Table(name="TEST_CHILD") > public class TestChild implements Serializable { > > @Id > @Column(name="ID") > @SequenceGenerator(name="testChildIdSeq", > sequenceName="TEST_CHILD_SEQ") > @GeneratedValue(generator="testChildIdSeq", > strategy=GenerationType.SEQUENCE) > private Integer id; > > @ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY) > @JoinColumn(name="FKEY") > private TestMaster master; > > @Column(name="FIELD_DATA") > private String fieldValue; > > //public getter/setters > > --------------------------------- > > > > TestMaster master = entityManager.find(TestMaster.class, 6); > > //WORKING WITH DETACHED OBJECT > > master.getChildren().clear(); > > > //REPLACING CHILD OBJECTS wITH NEW INSTANCES BUT WITH SAME PRIMARY KEY > TestChild child = new TestChild(); > child.setId(12); //primary key of child that exists already > child.setFieldValue("CHILD1-Changed"); > master.getChildren().add(child); > child.setMaster(master); > > TestChild child2 = new TestChild(); > child2.setId(11); //primary key of child that exists already > child2.setFieldValue("CHILD2-Changed"); > master.getChildren().add(child2); > child2.setMaster(master); > > > entityManager.getTransaction().begin(); > > entityManager.merge(master); > > entityManager.getTransaction().commit(); >
