-->
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.  [ 6 posts ] 
Author Message
 Post subject: Ternary associations with cascading delete
PostPosted: Thu Sep 18, 2003 8:28 am 
Newbie

Joined: Sun Sep 14, 2003 4:55 pm
Posts: 2
Hello,

can you give me a "cookbook recipe" for implementing a ternary association with support for cascading deletion when an object at any of its ends is deleted?

Here is what I want to achieve (based on the brief example from the manual):

1. The entities "employer" and "employee" are associated with a "contract", which might include additional attributes.
2. If an employer is deleted, all of her contracts are also deleted (cascade). The deletion should, if possible (?), occur by issuing an SQL statement like "delete from contract where employerID=?", rather than by loading up each contract and issuing "delete from contract where id=?" in a loop.
3. Likewise, if an employee is deleted, all of her contracts are deleted. By analogy, "delete from contract where employeeID=?" should do the job.

Hibernate experts, how would you suggest implementing this kind of relationship? I tried with two many-to-one mappings, but that approach seems to force me to manage the deletion of dependent "contracts" myself, and the deletions then occur by contract id.

While deleting contracts by cascade might sound silly, there are other situations where the described behavior is needed. If you think otherwise, I would be interested what alternatives there are on the conceptual level.

Best regards -
Jan Ploski


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 21, 2003 10:16 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Try using a composite-element, with two <many-to-one>s.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2004 11:52 pm 
Newbie

Joined: Fri Jan 30, 2004 6:48 pm
Posts: 12
I am having the same problem as above, and I have tried both approaches to the ternary association mapping problem. If I delete one of the "ends" of the association without removing the entry in the association table, I get a referential integrity violation.

Conceptually, I have two Nodes with a Connection between them. Node 1 has a group of Nodes that it can access (Map with the "accessible-Node" being the key and the Connection being the value). I can't delete that accessible-Node without first removing it from Node 1's Map. I shouldn't have to manage this relationship myself... yet the same problem happens when I try to make a composite-element with two many-to-ones as properties. Everything works except when I try to delete the accessible-Node first.

Any ideas on what I'm doing wrong?
here is my XML snippet:
<set
name="connections"
lazy="false"
inverse="false"
cascade="all"
sort="unsorted"
>

<key
column="fromNodeID"
/>

<composite-element
class="NodeConnection"
>

<many-to-one
name="Connection"
class="Connection"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="channel"
/>

<many-to-one
name="toNode"
class="Node"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="toUser"
/>

</composite-element>

</set>

(deleting the toNode object before removing it from fromNode's Set (or Map) causes ref. integrity error)

thanks in advance
Justin


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 03, 2004 11:44 am 
Newbie

Joined: Fri Jan 30, 2004 6:48 pm
Posts: 12
Is there a code example somewhere that might show one of the two approaches to ternary mapping along with cascade-delete?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 03, 2004 12:48 pm 
Newbie

Joined: Fri Jan 30, 2004 6:48 pm
Posts: 12
gavin (or anyone in the know),
when you said to "Try using a composite-element, with two <many-to-one>s." was that because it is impossible to get cascade-delete working with the map index approach, or because it is just better to do it via composite-element? I am looking at the association table that hibernate generates in both use cases, and they seem functionally equivalent. There are three columns each with a FK constraint to their respective entities. I can delete the entity holding the reference to this association in its Map, but I get the above error if I try to delete either of the other two entities that does not have a reference to the association table. Am I missing something obvious here? How should I expect hibernate to handle this sort of event? Is this model fundamentally flawed?
Any insight is much appreciated!
Justin


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2004 6:41 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
jcasp wrote:
deleting the toNode object before removing it from fromNode's Set (or Map) causes ref. integrity error

To let Hibernate manage this youmust have a bidir relationship with proper cascade. Check the parent child relationship and the http://www.hibernate.org/Documentation/InsideExplanationOfInverseTrue

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.