-->
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.  [ 6 posts ] 
Author Message
 Post subject: Parent Child Relation mapping
PostPosted: Wed Aug 30, 2006 4:16 pm 
Newbie

Joined: Fri Aug 04, 2006 12:27 am
Posts: 4
[1]Hibernate version:[/b] 3.0

[2]Mapping documents:[/b] I need them :-( from here

[3] Problem.

Following is the table structure i have for the parent child relation.


Table Name : Term

and following is the structure of it.
_ _ _ _ _ _ _ _ _ _ _ _ _
|Term_id Number PK
Term_text Varchar
Active_sw Varchar



There can be a parent child relation between the different terms and the data modeller represented it in the following way.

Table Name : Term_map

and following is the structure of it.

-----------------------------------------
|
|Parent_term_id Number PK,Fk1
|
|Child_term_id Number PK,Fk2
---------------------------------------------


now my problem is how do i do the mapping for the child elements of a parent..do i need 2 domain objects for this tables or would one be sufficient.


Is there a possible way of getting the hibernate mapping for this kind of table structure or do i need to ask the Data modeller to have everything in one table and have an other column for the parent_id?

please help.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 4:29 pm 
Newbie

Joined: Wed Aug 30, 2006 3:17 pm
Posts: 11
Is there a reason you want to have a separate table to keep track of the relationships? Unless you are planning for this to be a many-to-many association (which really isn't a parent-child relationship), or if you're trying to be compatible with an existing database whose schema you can't change, it should be possible just to have a nullable "parent_id" foreign key field in the first (and in that case, only) table, in which case you can just follow the "Parent-child" example in the Hibernate manual almost verbatim.

_________________
Rich Eggert


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 31, 2006 10:17 am 
Newbie

Joined: Fri Aug 04, 2006 12:27 am
Posts: 4
I will try to convince the Datamodeller but in case if he doesnt agree..is there still away where we can map this 2 tables.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 31, 2006 10:49 am 
Newbie

Joined: Wed Aug 30, 2006 3:17 pm
Posts: 11
I believe what you're describing is a one-to-many relationship with a join table, as described here:
http://www.hibernate.org/hib_docs/v3/re ... l-join-12m

However, unlike the example provided there, you're using a self-referential association, i.e., the parent and child tables/classes are the same. I could be wrong, but I think the way to do this is to just do both the parent and child mappings within the same class. Something like this might work:

Code:
<class name="Term" table="Term">
    <id name="id" column="Term_id">
        <generator class="native"/>
    </id>

    <set name="children" table="Term_map">
        <key column="Parent_term_id"/>
        <many-to-many column="Child_term_id" unique="true" class="Term"/>
    </set>

   <join table="Term_map" inverse="true" optional="true">
        <key column="Child_term_id"/>
        <many-to-one name="parent" column="Parent_term_id" not-null="true"/>
    </join>

</class>


(I've left out the property mappings for the non-key fields for the sake of simplicity).

Note that I just copied the example from the above documentation page and changed it around to fit your scenario. I haven't actually tried doing something like this, so I'm not certain that it works. It should be worth a try, though.

Also note that this mapping treats Child_term_id as the primary key of the Term_map table. Unless you plan for a child to be able to have more than one parent, it isn't necessary for Parent_term_id to be part of the primary key.

_________________
Rich Eggert


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 01, 2006 11:34 am 
Newbie

Joined: Fri Aug 04, 2006 12:27 am
Posts: 4
I think what u r saying might really work...i still havnt tried it since we are still trying to convert the datamodel to the actual structure.

The questions i have is in the following why is it a many-to-many relation?it should be one-to-many right?

<class name="Term" table="Term">
<id name="id" column="Term_id">
<generator class="native"/>
</id>

<set name="children" table="Term_map">
<key column="Parent_term_id"/>
<many-to-many column="Child_term_id" unique="true" class="Term"/>
</set>


<join table="Term_map" inverse="true" optional="true">
<key column="Child_term_id"/>
<many-to-one name="parent" column="Parent_term_id" not-null="true"/>
</join>

</class>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 05, 2006 1:15 pm 
Newbie

Joined: Wed Aug 30, 2006 3:17 pm
Posts: 11
To be honest, I was just copying what they had in the example. However, I believe the reason for it is that since you are using a join table, from a database point of view there's nothing stopping two different parent objects from claiming the same child as their own (which would happen if Term_map contained two rows with the same child_term_id). I think the unique="true" attribute is what actually makes it a one-to-many relationship.

Or it might just be a typo in the example. ;-)

_________________
Rich Eggert


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