-->
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: Many-to-many mapping with 2 objects of same class
PostPosted: Tue Feb 19, 2008 11:32 am 
Newbie

Joined: Wed Apr 12, 2006 7:02 am
Posts: 7
Location: Southampton, UK
Hibernate version:3.2.3

Name and version of the database you are using:mysql 5

Hello,

is it possible to have a bi-directional many-to-many relationship for objects of the same class?

i.e. can an Apple be related to many other Apples?

We have seen one solution with 2 Sets:

Code:
<class name="apple" class="Apple" table="apple">
     <set name="applesA"  table='apple_link">
         <key column="apple_a_id"/>
        <many-to-many column="apple_b_id" class="Apple"/>
    </set>
    <set name="applesB" table="apple_link">
        <key column="apple_b_id"/>
        <many-to-many columm="apple_a_id" class="Apple"/>
    </set>
</class>


We would like a solution similar to this but only have one Set defined. Is this possible?

Thanks in advance!


Top
 Profile  
 
 Post subject: Re: Many-to-many mapping with 2 objects of same class
PostPosted: Tue Feb 19, 2008 11:36 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
I don't see why it shouldn't be. One set has enough information for HB so that it know how to persist the relation. Are you having a problem with it?

Farzad-


Top
 Profile  
 
 Post subject: Re: Many-to-many mapping with 2 objects of same class
PostPosted: Tue Feb 19, 2008 12:09 pm 
Newbie

Joined: Wed Apr 12, 2006 7:02 am
Posts: 7
Location: Southampton, UK
farzad wrote:
I don't see why it shouldn't be. One set has enough information for HB so that it know how to persist the relation. Are you having a problem with it?


Yes, the problem is we want the relationship to be bi-directional.

Code:
public class Apple {

    private Set<Apple> apples = new HashSet<Apple>();

    public Set<Apple> getApples() {
        return apples;
    }

    public void setApples(Set<Apple> apples) {
        this.apples = apples;
    }
}


So we have the following test, something like this :

Code:
Apple apple1 = new Apple();
Apple apple2 = new Apple();

session.save(apple1);
session.save(apple2);

apple1.getApples().add(apple2);
session.update(apple1);

session.evict(apple1);
session.evict(apple2);

apple1 = session.load(Apple.class, apple1.getId);
apple2 = session.load(Apple.class, apple2.getId);

assertTrue(apple1.getApples.contains(apple2));
assertTrue(apple2.getApples.contains(apple1)); // WE WANT THIS TO WORK BUT IT DOESN'T


Top
 Profile  
 
 Post subject: Re: Many-to-many mapping with 2 objects of same class
PostPosted: Tue Feb 19, 2008 5:28 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
What you are testing here does not make sense since a relation has a direction. In your example apple1 has a relation with apple2, and apple2 has the reverse side of that relation. If you like to keep test this relation in apple2 you do need to define two sets, one representing incoming relations and the other outgoing relations. One of these relations should have inverse=true so that hibernate is not confused which one is which.

If the semantics of this relation is that when apple1 is linked to apple2 it also means apple2 is linked to apple1 (as in case in bidirectional edges of a graph) then you do need to add apple1 to apple2's collection too, resulting in two records in the joint table per relation.


I hope this helps.


Farzad-


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.