-->
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.  [ 7 posts ] 
Author Message
 Post subject: child table column name different from referred fk column
PostPosted: Fri Mar 05, 2004 5:02 am 
Newbie

Joined: Fri Mar 05, 2004 2:52 am
Posts: 15
hi,
I am using hibernate 2.1 with sybase.
Suppose I have 2 tables,(simplified to a large extent)

parent
======
id int
name varchar

suppose composite key here is (id,name)

child
====
child_id int
parent_id int (refers id of parent)
name varchar

suppose composite key here is (child_id,parent_id,name)

the mapping snippet for child is,


<composite-id name="childPK" class="Child" >
<key-property name="childId" column="child_id"/>
<key-property name="childName" column="name"/>
<key-many-to-one name="parent" class="Parent" >

<column name="id" /> ???????
</key-many-to-one>
</composite-id>


since the column name in "child" table "parent_id" is different from column name "id" in "parent" table (which it refers), how to tell to hibernate ? tag <column name="id" /> doesn't have a syntax by which I can tell to hibernate that this column will refer the column of parent table,

something like <column name="parent_id" referred_column_name="id" />

any help is greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 5:04 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Just use column_name="parente_id", a many-to-one will always refer to the parents id property.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 9:30 am 
Newbie

Joined: Fri Mar 05, 2004 2:52 am
Posts: 15
michael wrote:
Just use column_name="parente_id", a many-to-one will always refer to the parents id property.



thanks michael!
but it is still unclarified.

<key-many-to-one name="parent" class="Parent" >

<column name="id" /> here Parent table column name(which is id) has to be given,right???????it worked for me with this
</key-many-to-one>


consider this scenario,for different ids of child referring to different ids of parent (with different names).how will hibernate determine which id of child refers which id of parent (under which name).
is sequence of tags taken into consideration??


parent
======
id int
id_other int
name varchar

suppose there is a key here as (id,id_other,name)


child
====
child_id int
child_id_refer_parent int (refers id of parent)

child_id_other int (refers id_other of parent)
name varchar

suppose composite key here is (child_id,child_id_other,parent_id)

the mapping snippet for child will be,


<composite-id name="childPK" class="Child" >
<key-property name="childId" column="child_id"/>

<key-many-to-one name="parent" class="Parent" >

<column name="id" /> ?? here name is "id" but how to say it is referred by "child_id_refer_parent"
<column name="id_other" /> ?? here name is "id_other" but how to say it is referred by "child_id_other"
</key-many-to-one>
</composite-id>


thanks for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 11:10 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
<column> refer to an SQL column, thus the name attribute means SQL column name (it has to be child_id_refer_parent etc)
In case of composite id, the order is important, Hibernate will use it.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 06, 2004 1:22 am 
Newbie

Joined: Wed Feb 11, 2004 6:04 pm
Posts: 11
Location: Houston
It seams we have the exact problem - have you found a solution ?
here is my one http://forum.hibernate.org/viewtopic.php?t=928686

_________________
B-)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 07, 2004 12:54 am 
Newbie

Joined: Wed Feb 11, 2004 6:04 pm
Posts: 11
Location: Houston
Hi krovrav, if I understand emmanuel rigth and we look at your example with

parent
======
id int
id_other int
name varchar

suppose there is a key here as (id,id_other,name)


child
====
child_id int
child_id_refer_parent int (refers id of parent)

child_id_other int (refers id_other of parent)
name varchar
suppose composite key here is (child_id,child_id_other,child_id_refer_parent)

we have to make the order of the composite key like
(child_id_refer_parent,child_id_other,child_id) and than map it as

<key-many-to-one name="parent" class="Parent" >

<column name="child_id_refer_parent" />
<column name="child_id_other" />
</key-many-to-one>

Hibernate will than map the first in this list - "child_id_refer_parent" to the first in the parent composite key, i.e. "id" and the second in this list "child_id_other" to the second in the parent composite key ie. "id_other"

Can any confirm this is how it works ?

_________________
B-)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 07, 2004 1:53 am 
Newbie

Joined: Wed Feb 11, 2004 6:04 pm
Posts: 11
Location: Houston
One answer is given in the FAQ's from the forum http://www.hibernate.org/74.html#A2 about "How do I set a many-to-one association to an Entity having a composite id?" It coul dbe extended to explain how this works when the column names are not the same in th eparent and child.

Also I miss a FAQ about "How do I set a one-to-many association to an Entity having a composite id? in a "set" block ?

Thanx to all of you. This is really a great product and I enjoy working with it.

B-)

_________________
B-)


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