-->
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.  [ 3 posts ] 
Author Message
 Post subject: Children persistent collection management
PostPosted: Mon Nov 21, 2005 2:41 pm 
Newbie

Joined: Mon Nov 21, 2005 2:24 pm
Posts: 8
I am trying to find out a way to easy manage a children collection within a parent object by adding, removing or changing the entries in a collection a children object referenced by a parent object.
Is there a way to make these changes to the children Set persistent?
My case is a one to many parent child relationship, with inverse= true and the the constraint in the foreign key column of the child table forced to be not null.
The only way it seems to work is to remove a child object from the children Set and deleting it from he database, like this:

parent.geTheCildrenSet().remove(child);
session.delete(child);

I would like somehing like:

parent.getTheChildrenSet.remove(aChild);
parent.getTheChildrenSet.add(new Child());
...
...
session.saveOrUpdate(parent);

Is this possible one way or another?

Many thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 21, 2005 2:48 pm 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
You need a cascade="all-delete-orphan" on the collection mapping. Refer to the Hibernate documentation or the book for more information.
Basically this means any saves/updates to the parent will be cascaded to the children as well as removal of any object from the parent collection will delete the child row that was just removed.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 22, 2005 7:18 am 
Newbie

Joined: Mon Nov 21, 2005 2:24 pm
Posts: 8
It looks like it is working now.

Here are the data related to the two classes:

Parent:
........................
<set name="RuleContextKws" inverse="true" cascade="all-delete-orphan" lazy="false">
<key column="rule_id"/>
<one-to-many class="RuleContextKw" />
</set>
.........................

Child: (the child has a composite key but this should not be relevant)
....................................
<composite-id name="Id" class="RuleContextKWPK">
<key-many-to-one
name="RuleId"
column="rule_id"
class="Rule"
/>
<key-property
name="Keyword"
column="keyword"
type="string"
length="50"
/>
</composite-id>
......................................

The code just creates a session when loading the parent object from the DB and then it closes it. By having now the reference of the whole object the client just plays around with the Set of children adding and/or removing entries in the collection.
Eventually a new session is open along with a new transaction and the update (or save) parent method is invoked.


Code:
Transaction tx = null;
Session session = null;
Integer id = null;
try {
   RuleInfoDAO rules = RuleInfoDAO.getInstance();
   session = rules.createNewSession();
   tx = rules.beginTransaction(session);
//..........there is some code which detects if we need to save or update
if(.....)
       id = rules.save(rule, session);//in case of new parent object
else
       rules.update(rule, session);//in case of an existing parent object
         rules.commitTransaction(tx);
         rules.closeSession(session);


About the database I don't know if it is related but verything started to work when I added the cascade constraint on the foreign key to the parent table.
I hope this can help even the others who are struggling with this kind of issues.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.