openjpa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ksarum <kumar.m...@gmail.com>
Subject Issue updating child object changes by doing merge/find update on parent object
Date Fri, 22 Oct 2010 14:45:50 GMT

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. 

Is my approach wrong? 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();
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Issue-updating-child-object-changes-by-doing-merge-find-update-on-parent-object-tp5662775p5662775.html
Sent from the OpenJPA Developers mailing list archive at Nabble.com.

Mime
View raw message