-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Hibernate & Spring: Replace child list entities in an entity
PostPosted: Tue Mar 09, 2010 4:55 am 
Newbie

Joined: Sat Sep 13, 2008 5:16 am
Posts: 12
Hi,

I have an entity with a one-to-many-relationship:

Code:
MyEntity{
List<Child> children;

get and set children..
}


I want to know how I would make Hibernate replace my existing children with a new list of children.
I thought it would be enough to use the setter method of MyEntity (if within a transaction) and then update
MyEntity. But it does not work.
I'm using Hibernate with spring and my 'save-to-database'-method is annotated with the Transactional attribute.
However, the actual call to the set method is done in a separate method (called from 'save-to-database'-method).
This separate method is not annotated. Should it be?
What actually happens right now is that when using the setChildren method, I ADD more children to my existing list. I would like
to replace to old list with the contents of the new list.

/best regards, Håkan Jacobsson, Sweden

_________________
/best regards, Håkan Jacobsson


Top
 Profile  
 
 Post subject: Re: Hibernate & Spring: Replace child list entities in an entity
PostPosted: Tue Mar 09, 2010 9:29 am 
Beginner
Beginner

Joined: Thu Oct 16, 2008 11:19 am
Posts: 23
Location: Norway
I'm not quite sure I understand what is going on, but what do you have in the "cascade" field for that set in the mapping file for MyEntity? Here is an example of what works for me:

<set name="children" table="CHILD" inverse="true" cascade="delete,save-update,delete-orphan" lazy="true">
...

This will delete all children in the set if MyEntity is deleted, save and update the children of the set, and delete children that have been removed from the set and have become orphans, respectively.

I hope this was of help! :)


Top
 Profile  
 
 Post subject: Re: Hibernate & Spring: Replace child list entities in an entity
PostPosted: Thu Mar 11, 2010 4:23 am 
Newbie

Joined: Sat Sep 13, 2008 5:16 am
Posts: 12
Hi,

I have cascadetype=ALL. So delete-orphan should do the trick then?

_________________
/best regards, Håkan Jacobsson


Top
 Profile  
 
 Post subject: Re: Hibernate & Spring: Replace child list entities in an entity
PostPosted: Thu Mar 11, 2010 5:23 am 
Newbie

Joined: Sat Sep 13, 2008 5:16 am
Posts: 12
Hi again,

No, this did not work.
The problem is not that the children become orphans. The children still belongs to MyEntity. And the children
become duplicated when I save MyEntity twice. In spite of using MyEntity.setChildren(new List with children) each time before saving.
I'm using JPA annotations - and like I said, I have CascadeType=ALL.
The children list is fetched lazily and I do not retrieve my children list in MyEntity without using a join.
Help much appreciated!

_________________
/best regards, Håkan Jacobsson


Top
 Profile  
 
 Post subject: Re: Hibernate & Spring: Replace child list entities in an entity
PostPosted: Fri Mar 12, 2010 5:54 am 
Beginner
Beginner

Joined: Thu Oct 16, 2008 11:19 am
Posts: 23
Location: Norway
pussmun wrote:
I have cascadetype=ALL. So delete-orphan should do the trick then?
Yes, the attribute "delete-orphan" is not included in "all", but evidently this was not the solution to your problem.

pussmun wrote:
The problem is not that the children become orphans. The children still belongs to MyEntity. And the children
become duplicated when I save MyEntity twice. In spite of using MyEntity.setChildren(new List with children) each time before saving.
I'm using JPA annotations - and like I said, I have CascadeType=ALL.
The children list is fetched lazily and I do not retrieve my children list in MyEntity without using a join.
Perhaps it would be easier to help if you posted the relevant excerpts from the mapping files and domain classes?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.