-->
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.  [ 9 posts ] 
Author Message
 Post subject: Persons & Friends -- newbie mapping question
PostPosted: Mon May 09, 2005 5:30 pm 
Beginner
Beginner

Joined: Mon May 09, 2005 5:26 pm
Posts: 21
Hello,

can anyone explain to me how one might implement the classic "Person/Friend" problem using Hibernate?


in relational land a "Person" table has an ID and some info and a "Friend" table is a self-join of the "Person" table -- that is, the "Friend" table has two colums holding the ID's of two persons. in object land, a class Person has a field "friends" of type Set, into which are placed objects of type Person.

I'm *far* from a Hibernate wiz, but a person on our development team who is using Hibernate claims it can't be done -- I find this difficult to believe.

Bonus question: anyone know how to do the above using the new java/hibernate annotations?

thanks much,

-don.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 10, 2005 4:07 pm 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
It can definitely be done:
Something like that:

Code:
<class name="Person" table="person" lazy="true">
        <id name="id" column="id" type="java.lang.Long">
            <generator class="hilo"/>
        </id>       
   <set name="friends" table="person_friends" lazy="true" inverse="true">
         <key column="parent_id"/>
      <many-to-many class="Person" column="child_id"/>
   </set>       
</class>



Hope that helps.

Vincent Giguère

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 10, 2005 11:47 pm 
Beginner
Beginner

Joined: Mon May 09, 2005 5:26 pm
Posts: 21
Thanks much. I'll pass it along to said developer and give 'er a try.


Top
 Profile  
 
 Post subject: How do you self-join in Hibernate?
PostPosted: Thu Feb 02, 2006 8:51 am 
Newbie

Joined: Thu Feb 02, 2006 8:45 am
Posts: 4
Location: Boston
Sorry, but I don't see how this is a "self-join", since there is a second table, person_friends.

I've been looking through the Hibernate In Action book and the Hibernate Quickly book, and googling, but I don't yet see how to do this.

Does anyone know?

Ben


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 09, 2006 1:18 am 
Beginner
Beginner

Joined: Mon May 09, 2005 5:26 pm
Posts: 21
Hi Ben,

It's a self-join in object land for sure -- persons reference persons. Since it's a many-to-many, there *must* be another table as Vincent correctly noted. This isn't a Hibernate thing -- there's just no other way to construct the tables.

To try to use a single table, it'd have to look like:
Person
--------
int personID
String name
int friendID1
int friendID2
..


how many friends would you like to allow?

-don.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 09, 2006 5:45 am 
Newbie

Joined: Thu Feb 02, 2006 8:45 am
Posts: 4
Location: Boston
An infinite number, as is commonly done.

Ben


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 04, 2006 9:00 am 
Newbie

Joined: Thu Dec 01, 2005 12:23 pm
Posts: 19
Got also an example how to achieve the same result using Hibernate EJB3 Annotations??

Preferably for a class that got a single parent instance of itself and and a child collection of itself:

Code:
public class Person {

   private String id;

   private Person parent;

   private Collection<Person> childs;
}


Adding such an example to the annotations documentation surely would not be a bad thing. :)

- bitbyter



vgiguere wrote:
It can definitely be done:
Something like that:

Code:
<class name="Person" table="person" lazy="true">
        <id name="id" column="id" type="java.lang.Long">
            <generator class="hilo"/>
        </id>       
   <set name="friends" table="person_friends" lazy="true" inverse="true">
         <key column="parent_id"/>
      <many-to-many class="Person" column="child_id"/>
   </set>       
</class>



Hope that helps.

Vincent Giguère


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 24, 2007 8:04 am 
Newbie

Joined: Thu May 24, 2007 7:18 am
Posts: 2
Can you please share the sample code to achive following using annotations.

public class Person {

private String id;

private Person parent;

private Collection<Person> childs;
}

bitbyter wrote:
Got also an example how to achieve the same result using Hibernate EJB3 Annotations??

Preferably for a class that got a single parent instance of itself and and a child collection of itself:

Code:
public class Person {

   private String id;

   private Person parent;

   private Collection<Person> childs;
}


Adding such an example to the annotations documentation surely would not be a bad thing. :)

- bitbyter



vgiguere wrote:
It can definitely be done:
Something like that:

Code:
<class name="Person" table="person" lazy="true">
        <id name="id" column="id" type="java.lang.Long">
            <generator class="hilo"/>
        </id>       
   <set name="friends" table="person_friends" lazy="true" inverse="true">
         <key column="parent_id"/>
      <many-to-many class="Person" column="child_id"/>
   </set>       
</class>



Hope that helps.

Vincent Giguère


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 25, 2007 5:40 am 
Newbie

Joined: Thu Dec 01, 2005 12:23 pm
Posts: 19
The following code should do what you are looking for!

Code:
@Entity
@Table(name = "TBL_PERSON")
public class Person {

    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    @Column(name = ID)
    private String id;

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "parent")
    private Person parent;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "parent")
    @Cascade(value = { org.hibernate.annotations.CascadeType.ALL,
            org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
    private Collection<Person> children = new LinkedList<Person>();
}



HTH

- bitbyter

chandramohan_murkute

chandramohan_murkute wrote:
Can you please share the sample code to achive following using annotations.

public class Person {

private String id;

private Person parent;

private Collection<Person> childs;
}


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