-->
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: Parent / Child Relation ship ????
PostPosted: Tue Mar 30, 2004 4:54 pm 
Newbie

Joined: Tue Mar 23, 2004 12:21 pm
Posts: 11
Hi All,
I have to design a Data Access Service using hibernate. My case is something like this:
I have a Category table (and a category object ofcourse). The Category can have many subcategories and the subcategories can have their own subcategories. Also Category and subcategory are represented by the same table and hence the same object. The table has many fields out of which two most important are
-CategoryId
-parentId
The categoryId of a category(or subcategory) is parentId of its subcategory and the toplevel category has a fictious parentId (say 'root').
Also the lowest level element in the hierarchy is a 'link' object , defined by another table(and hence another object). The 'link' object is associated to a category by a categoryId .My question is how do I design the parent child relationship. I tried looking at the documentation but it seems more like a reference(I am only a begineer with hibernate). As far as my understanding goes , I can use collections beacuse according to the documentation "Collections may not contain other collections" .If anyone of you can provide any headsup or sample code , it will be greatly appreciated.

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 30, 2004 5:04 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
can you give "simple" tables descriptions + foreign keys?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 30, 2004 5:22 pm 
Newbie

Joined: Tue Mar 23, 2004 12:21 pm
Posts: 11
The category table has a primary key called 'CategoryId' . This key is also a foreign key.

There is another table called LINK. The primary key for this table is 'LinkID' and it is associated with the category table through a foreign key (categoryID). The link is the lowest level member in the hierarchy. It will not have any children but will have a category/subcategory as its parent.

Did i provide the information you wanted?

I appreciate your help.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 30, 2004 6:29 pm 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
Saurabh wrote:
The category table has a primary key called 'CategoryId' . This key is also a foreign key.

There is another table called LINK. The primary key for this table is 'LinkID' and it is associated with the category table through a foreign key (categoryID). The link is the lowest level member in the hierarchy. It will not have any children but will have a category/subcategory as its parent.



Saurabh,

From what you have described it appears like you are maintaining parent-child relationship for the same set of data in a separate table. The mapping for this could be done in two different ways.

If each parent can have only one child and each child can have only one parent, the relationship would be a one-to-one.

If each parent can have multiple children and each child can have only one parent it is a one-to-many.

If each parent can have only one child but each child can have multiple parents, the relationship would be a many-to-one.

If each parent can have multiple children and each child can have multiple parents, the relationship would be a many-to-many.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 30, 2004 6:56 pm 
Newbie

Joined: Tue Mar 23, 2004 12:21 pm
Posts: 11
gpani,
thanks for responding. If you look at my first post. The situation is more complicated than this. Please read my first post. I appreciate the help.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 30, 2004 7:06 pm 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
Saurabh wrote:
gpani,
thanks for responding. If you look at my first post. The situation is more complicated than this. Please read my first post. I appreciate the help.


Saurabh,

I am sure the description in your initial post makes sense in some way but from the point of view that I am reading it, it seems a bit convoluted.

Check and see if the forum link below solves your problem.

http://forum.hibernate.org/viewtopic.php?t=929191&highlight=manytomany


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 30, 2004 10:08 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
Saurabh wrote:
gpani,
thanks for responding. If you look at my first post. The situation is more complicated than this. Please read my first post. I appreciate the help.


Here is the example which is closest to what you are trying to do:

http://www.hibernate.org/hib_docs/reference/html/collections.html#collections-s1-10

You basically have a tree of Category objects linked through the parent field, correct? You then want each Category object to have both a many-to-one mapping to the parent Category (may be null), and a one-to-many Set mapping to the child Categories (may be empty). This is almost identical to the given Node example.

Cheers,
Rob


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 31, 2004 2:02 am 
Newbie

Joined: Mon Mar 22, 2004 5:23 am
Posts: 18
Location: Bangalore
Saurav,
It appears to me , from ur description that the Category table has a self-referential integrity contraint(i.e. the foreign key of this table is in itself) and that there is a 1-M relationship between the Category and the Link tables.

I think u should use something like the following.

//classes
class Category{

private long categoryId;
private Set subcategories;
// other private fields


// getter-setter methods for the above fields



}

class Link{

private long linkId;
private Category category;

// other private fields

// getter-setter methods for the above fields

}

// Mappings


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="eg">
<class name="Category" table="CATEGORY" >
<id name="categoryId" column="CATEGORYID" type="long">
<generator class="hilo"/>
</id>
<property name=...>
<property name=...>
.
.
.
<set name="subcategories">
<key column="parentId"/>
<one-to-many class="Category"/>
</set>
<set name="links" inverse="true" >
<key column="categoryId"/>
<one-to-many class="Link"/>
</set>
</class>

<class name="Link" table="LINK" >
<id name="linkId" column="LINKID" type="long">
<generator class="hilo"/>
</id>
<many-to-one name="category" class="Category" column="CATEGORYID"/> <!-- here the CATEGORYID is the foreign key -->
<property name=...>
<property name=...>
.
.
.
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 31, 2004 2:06 am 
Newbie

Joined: Mon Mar 22, 2004 5:23 am
Posts: 18
Location: Bangalore
Saurabh,
please also add the following field and its corresponding getter/setter in ur Category class

private Set links;

thanks
santosh


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.