-->
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: unidirectional parent child cleanup.
PostPosted: Tue Jan 10, 2006 11:33 am 
Newbie

Joined: Wed Jan 04, 2006 11:06 am
Posts: 3
Hello again, I was wondering if anyone has an answer for this question I previously posted.

http://forum.hibernate.org/viewtopic.ph ... highlight=

I am switching over a dao from ejb2x to hibernate3 and cannot replicate the functionality that was available with ejb2.

Summary of the problem. Have object model like such.

class User {
String id;
Role userrole;
}

class Role {
String id;
Privilege[] roleprivs;
}

class Privilege{
String id
}

Notice i have not included other simpler properties.
User has a unid directional many-to-one with role. User table has an fk column which is the pk of the role table (simple enough) .

<many-to-one name="userrole" cascade="save-update" column="userrole_fk" class="org.user.role.data.Role"/>

All works except for case where you delete a role that a user has referenced and i dont know how to update the user table to make the fk column null. Ejb2 automattically cleaned up this fk even though it was unidirectional.

Same scenario with unidirectional many-to-many between role and privilege.
<array name="privies"
table="role_privies_link"
cascade="persist">
<key column="role_id" />
<list-index column="sorterx"/>
<many-to-many column="privies_id" class="org.user.privilege.data.Privilege" foreign-key="id" />
</array>

During delete of a Privilege it will not remove entry in link table if one exists between some role and the delete privilege.

I do not want a child role to know about a user, and i do not want a child privilege to know about a role. So i think a bi directional relationship is out of the question because a relationship needs a name value, meaning a parent /parents property on the child object.

I truely thought hibernate would handle this cleanup but all cascading (if added in) in case of unidrectional is from parent to child.


Top
 Profile  
 
 Post subject: same problem
PostPosted: Tue Jan 10, 2006 11:59 am 
Beginner
Beginner

Joined: Sat Oct 18, 2003 8:00 pm
Posts: 22
I have the same problem and posted it here:
http://forum.hibernate.org/viewtopic.php?t=953886
Although you maybe explained it in a better way :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 10, 2006 6:50 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
Hibernate does not pass any actions automatically. You must specify your cascade method properly.

if A and B are linked.
and you want to delete B.
than B must have a cascade="delete"
and if you want to save A when B is saved:
cascade="save-update,delete"

If you do not want to have stale data in the session, you must as opposed to EJB 2
A.setB(null)
B.setA(null)

Regards Sebastian

Please rate, if this helped.

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 11, 2006 6:53 am 
Newbie

Joined: Wed Jan 04, 2006 11:06 am
Posts: 3
I dont think helps, because as described above all relationships are uni-directional, and although A has a B, B does not have a A. And more so B has C[] but indiiviual C does not have reference to B.
That is referering to model above.
A = User
B = Role
C = Privilege

Also if maybe i have misinteruted your reply, you save that cascade should be delete. I am familar with this but you need to be more specific and give actual example mapping file because its a specific problem.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 11, 2006 7:07 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
when A has a link to B and you want to delete B
than you must do
a.setB(null)
or
a.getBs().remove(a);
to have your session up to date. Then you can make
session.delete(b)
You may use a query to set all userrole_fk to null where a.b=b
after this you should clear your session.

have a look in the thread http://forum.hibernate.org/viewtopic.php?t=953886

where I allready posted a reply.

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


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.