-->
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.  [ 4 posts ] 
Author Message
 Post subject: Recursive relationship with lookup table help
PostPosted: Wed Aug 15, 2007 6:18 pm 
Newbie

Joined: Wed Aug 15, 2007 5:56 pm
Posts: 2
First off I am a hibernate noob and apologize for my ignorance. I have a User object which contains a set of parents (which are user objects) and children (which are user objects) The mapping is handled in a mapping table. it looks like this

Code:
mysql> desc user_relationship;
+-----------+---------+------+-----+---------+-------+
| Field     | Type    | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| parent_id | int(11) | NO   | PRI |         |       |
| user_id   | int(11) | NO   | PRI |         |       |
+-----------+---------+------+-----+---------+-------+


I am mapping users to parents and children using the following mapping:

Code:
<hibernate-mapping>
  <class name="User" table="user">
    <id name="id" type="int">
        <generator class="identity"/>
    </id>
    <property name="username" column="username" type="java.lang.String"/>

    <set name="children" table="user_relationship">
            <key column="parent_id"/>
            <many-to-many column="user_id" class="User"/>
        </set>

    <set name="parents" table="user_relationship" >
        <key column="user_id"/>
        <many-to-many column="parent_id" class="User"/>
    </set>
  </class>
</hibernate-mapping>

A problem arises when I try to save the users and their relationships to the DB. If userA is a parent of userB, hibernate attempts to insert the relationship between userA and userB which succeeds, then it tries to insert the relationship between userB and userA which obviously fails, because its the same relationship resulting in a unique constraint violation. I think I can get around it by never saving the child relationships. Without using annotations, is there a way I can make the children collection read only? If not, is there a better design that would accomplish this? I'm sure the whole recursive mapping using lookup tables has been done before somewhere.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 15, 2007 6:21 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Read the Javadoc of the attached files:

http://opensource.atlassian.com/project ... e/HHH-1395

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 15, 2007 7:23 pm 
Newbie

Joined: Wed Aug 15, 2007 5:56 pm
Posts: 2
thanks for the quick response. I haven't been thinking of my situation as a Tree because a User can have more than one parent. If I were to change your code to allow multiple parents, would it still work?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 15, 2007 8:36 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
No, that would not be a direct acyclic graph but a network. Some of the explosion operations apply to that as well but my code doesn't work with that out-of-box.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


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