-->
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: many-to-many self-referencing association
PostPosted: Thu Aug 19, 2004 3:25 pm 
Newbie

Joined: Thu Aug 19, 2004 3:15 pm
Posts: 4
I want to do this:

Person <---------> Person

If Person A is a friend of Person B, than B is a friend of A

A.getFriends().add(B);

List listA = a.getFriends();

List listB = b.getFriends();

ListA will contain Person B
List B will contain Pereson A.


Is this possible in Hibernate - I've read the forums and the docs but this is not addressed. Can't figure out how to do it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 3:38 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
just read about many to many...

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 4:20 pm 
Newbie

Joined: Thu Aug 19, 2004 3:15 pm
Posts: 4
Well, I've read it again and still can't figure it out. The problem is the relationship is not just self-referencing, many-to-many its also bidirectional. Please take a look at my first post if this is not clear.

Anyhelp - other than pointing to the documentation again - would be greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 4:35 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
if you want help
Quote:
- other than pointing to the documentation again -
,
read the red card and propose, at least, a document mapping

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 5:13 pm 
Newbie

Joined: Thu Aug 19, 2004 3:15 pm
Posts: 4
OK. Hopefully this will help:

I have only one entity in my data base, person. Here is the mapping for Person.

Code:
  <class name="com.orkut.Person" table="PERSON">
    <meta attribute="class-description">
      Represents a single Person in the Orkut Database.
      @author RMH
    </meta>

    <id name="id" type="int" column="PERSON_ID">
      <meta attribute="scope-set">protected</meta>
      <generator class="native"/>
    </id>

    <property name="firstName" type="string" not-null="true"/>

    <property name="lastName" type="string" not-null="true"/>
       
        <set name="friends" table="FRIENDS_LINK" >
   <key column="person_1_id"/>
            <many-to-many class="com.orkut.Person" column="person_2_id"/>
         </set>

  </class>

The class generated from this looks like this:
Code:
package com.orkut;

import java.io.Serializable;
import java.util.Set;
import org.apache.commons.lang.builder.ToStringBuilder;


/**
*       Represents a single Person in the Orkut Database.
*       @author RMH
*     
*/
public class Person implements Serializable {

    /** identifier field */
    private Integer id;

...

    /** persistent field */
    private Set friends;

  ...
    public Set getFriends() {
        return this.friends;
    }

    public void setFriends(Set friends) {
        this.friends = friends;
    }

    ...

}

I run one script to populate my database. Its very simple and it works
Code:

            tx = session.beginTransaction();

            Person rrose = new Person("Richard","Rose",new java.util.HashSet());
            session.save(rrose);

            Person dWater = new Person("David","Water",new java.util.HashSet());
            session.save(dWater);

            Person dSteel = new Person("David","Steel",new java.util.HashSet());
            session.save(dSteel);

            Person dWood = new Person("Don","Wood",new java.util.HashSet());
            session.save(dWood);

            rrose.getFriends().add(dWater);
            rrose.getFriends().add(dSteel);
            rrose.getFriends().add(dWood);
            dWater.getFriends().add(dSteel);
            dSteel.getFriends().add(dWood);

            // We're done; make our changes permanent
            tx.commit();

I than run a script to query the database. Its also simple:
Code:
            for (Iterator iter = session.find("from com.orkut.Person").iterator() ; iter.hasNext() ; ) {
                Person person = (Person)iter.next();
                outName(person.getFirstName()+" "+person.getLastName());
                for(Iterator friends = person.getFriends().iterator();friends.hasNext(); ){
                  Person friend = (Person)friends.next();
         outName("   "+friend.getFirstName()+" "+friend.getLastName());
                }
            }

The output from this query is as follows:
Code:
qtest:
     [java] Richard Rose
     [java]    David Steel
     [java]    David Water
     [java]    Don Wood
     [java] David Water
     [java]    David Steel
     [java] David Steel
     [java]    Don Wood
     [java] Don Wood

However, if the relationship was bidirectional than it should have looked like this (bold items were missing):
Code:
qtest:
     [java] Richard Rose
     [java]    David Steel
     [java]    David Water
     [java]    Don Wood
     [java] David Water
     [java]    David Steel
     [java]    Richard Rose
     [java] David Steel
     [java]    Don Wood
     [java]    Richard Rose
     [java] Don Wood
     [java]    David Steel
     [java]    Richard Rose


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.