-->
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: parent-chield relation not by primary key
PostPosted: Mon Aug 16, 2004 2:31 am 
Newbie

Joined: Mon Jul 12, 2004 6:13 am
Posts: 7
[b]Hibernate version:2.1.4[/b]

How can I design following relation.
I have parent table with fields id(PK),id2,...
I have chield table with feilds id2(PK),id3(PK),fld1,fld2,...
chield table should be linked with parent by id2 field (which is not Primary Key). how should be described relation in parent.hbm.xml
I tried many combinations like this:

<set
name="children"
lazy="true"
inverse="true"
>
<key>
<column name="id2" />
</key>
<one-to-many
class="com.*.persistence.Chield"
/>
</set>
but in any case HSQL query "select Parent FROM Parent left join fetch Chield"
leads to link between parent.id=chield.id2 (I need parend.id2=chield.id2)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 16, 2004 11:26 am 
Beginner
Beginner

Joined: Mon Aug 09, 2004 12:31 pm
Posts: 47
Location: New York, NY, USA
See this forum post & reply:

http://forum.hibernate.org/viewtopic.php?t=933550

_________________
--DP


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 16, 2004 11:42 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
It is not possible with one-to-many mapping. child should have a foreign key that references to the parent primary key.

Let's try to replace one-to-many mapping by many-to-one in parent:
Code:
<set name="children" lazy="true" inverse="true">
    <key>
        <column name="id2" />
   </key>
    <many-to-one class="com.*.persistence.Chield" property-ref="id2" />
/>
</set>


your child mapping should look like:
Code:
<composite-id>
    <key-many-to-one name="parent" column="id2"/>
    <key-property name="id3" column="id3" />
</composite-id>

<many-to-one name="parent" column="id2" insert="false" update="false"/>
<property name="id3" insert="false" update="false"/>

_________________
Leonid Shlyapnikov


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 16, 2004 11:52 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
if does not work, try this for child mapping:
Code:
<composite-id>
    <key-property name="id2" column="id2"/>
    <key-property name="id3" column="id3" />
</composite-id>

<property name="id2" column="id2" insert="false" update="false"/>
<property name="id3" column="id3" insert="false" update="false"/>

_________________
Leonid Shlyapnikov


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 16, 2004 11:55 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
and please use [code] tags for your mappings, it makes them more readable ;)

_________________
Leonid Shlyapnikov


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.