You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by ksarum <ku...@gmail.com> on 2010/10/22 22:44:10 UTC

Issues with openjpa in updating child objects in merge / find update

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();
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Issues-with-openjpa-in-updating-child-objects-in-merge-find-update-tp5664089p5664089.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Issues with openjpa in updating child objects in merge / find update

Posted by ksarum <ku...@gmail.com>.
I use openjpa 2.0. I havent included @version field in this example but I
tried that using a timestamp field but it didnt work as well. When I dont
use any of those according to documentation it says that value in @Id field
will be used to issue update or insert, but that is not happening in my
case.
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Issues-with-openjpa-in-updating-child-objects-in-merge-find-update-tp5664089p5694419.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Issues with openjpa in updating child objects in merge / find update

Posted by Rick Curtis <cu...@gmail.com>.
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 <ku...@gmail.com> 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();
>

Re: Issues with openjpa in updating child objects in merge / find update

Posted by venra <sv...@hotmail.com>.
Thanks Mark for your information on this. If you find anything else, please
let me know.

Thanks
-Venra
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Issues-with-openjpa-in-updating-child-objects-in-merge-find-update-tp5664089p5785128.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Issues with openjpa in updating child objects in merge / find update

Posted by Mark Struberg <st...@yahoo.de>.
Hi venra!

Glad it works for you, but be aware that I found a few other things with this new configuration which now doesn't work: 

.) Creating an entity
.) detach it 
.) add items to an @ElementCollection in your entity
.) merge it

should store the element collection to the database. But my database 1:n table is still empty :(

So I'm now digging into this atm.

To me it seems that the DetachedStateManager area needs a cleanup. The problem with this is that there are thousands of possible scenarios ...


LieGrue,
strub

--- On Mon, 11/29/10, venra <sv...@hotmail.com> wrote:

> From: venra <sv...@hotmail.com>
> Subject: Re: Issues with openjpa in updating child objects in merge / find update
> To: users@openjpa.apache.org
> Date: Monday, November 29, 2010, 2:45 PM
> 
> Mark,
> 
> I changed the mappings. Now it is working fine with the
> changes what you
> suggested. I really thankful to you. Thank you very much.
> 
> -Verna
> -- 
> View this message in context: http://openjpa.208410.n2.nabble.com/Issues-with-openjpa-in-updating-child-objects-in-merge-find-update-tp5664089p5784536.html
> Sent from the OpenJPA Users mailing list archive at
> Nabble.com.
> 


      

Re: Issues with openjpa in updating child objects in merge / find update

Posted by venra <sv...@hotmail.com>.
Mark,

I changed the mappings. Now it is working fine with the changes what you
suggested. I really thankful to you. Thank you very much.

-Verna
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Issues-with-openjpa-in-updating-child-objects-in-merge-find-update-tp5664089p5784536.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Issues with openjpa in updating child objects in merge / find update

Posted by venra <sv...@hotmail.com>.
Mark,

Thanks for your reply. I tried what you suggested but still not working. I
really appreciate if you could send me the sample code what you tried.
Thanks in advance.

Verna
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Issues-with-openjpa-in-updating-child-objects-in-merge-find-update-tp5664089p5784523.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Issues with openjpa in updating child objects in merge / find update

Posted by Mark Struberg <st...@yahoo.de>.
Hi Venra!

This is just a blind guess, but I got almost all my magic merge issues resolved by using the following option:

<property name="openjpa.DetachState" value="fetch-groups"/>

No idea if it helps in your case, but might be worth trying?

txs and LieGrue,
strub

--- On Fri, 11/26/10, venra <sv...@hotmail.com> wrote:

> From: venra <sv...@hotmail.com>
> Subject: Re: Issues with openjpa in updating child objects in merge / find update
> To: users@openjpa.apache.org
> Date: Friday, November 26, 2010, 6:37 PM
> 
> I was trying the same type of example but i can not get all
> the tables
> updated. did any one got it worked?
> 
> if so, please provide your sample code here. That would be
> really helpful
> for me. thanks.
> -- 
> View this message in context: http://openjpa.208410.n2.nabble.com/Issues-with-openjpa-in-updating-child-objects-in-merge-find-update-tp5664089p5778166.html
> Sent from the OpenJPA Users mailing list archive at
> Nabble.com.
> 


      

Re: Issues with openjpa in updating child objects in merge / find update

Posted by venra <sv...@hotmail.com>.
I was trying the same type of example but i can not get all the tables
updated. did any one got it worked?

if so, please provide your sample code here. That would be really helpful
for me. thanks.
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Issues-with-openjpa-in-updating-child-objects-in-merge-find-update-tp5664089p5778166.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.