-->
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.  [ 1 post ] 
Author Message
 Post subject: mapping parent-child when child's FK is not the parent's PK
PostPosted: Thu Nov 29, 2007 2:22 pm 
Newbie

Joined: Tue Oct 16, 2007 3:42 pm
Posts: 11
hi,

when we establish a parent child relationship, then normally the FK in the child table will be the PK of the parent table.

and in this case, i can map the relationship in the mapping file as follows:

The parent would map to the child as:
Code:
<id name="parentid" type="int" column="PARENTID"/>

<set name="children" inverse="true" cascade="all-delete-orphan"
sort="unsorted" mutable="true" optimistic-lock="true" embed-xml="true"> <key column="CHILDID" on-delete="noaction"/>
<one-to-many class="edu.nyu.hibernate.Child" not-found="exception" embed-xml="true"/>
</set>


The child would map to the parent as:
Code:
<id name="childid" type="int" column="CHILDID"/>

<many-to-one name="parent" class="edu.nyu.hibernate.Parent" column="PARENTID" insert="false" update="false" unique="false"
optimistic-lock="true" not-found="exception" embed-xml="true"/>


This I have verified that it works fine, but problem occurs when the FK in the child table is not the PK of the parent table.

In the hibernate documentation, the way to get around this is to use:

Code:
property-ref="propertyName"


which according to the documentation "Specifies that the foreign key refers to columns that are not the primary key of the orginal table. (Provided for legacy data.) "

My understanding is that I need to put this only in the child mapping file, but is this correct? Because when I do so, I get a java.sql.BatchUpdateException stating that:

insert into CHILD (column1, column2, ... ) values (value1, value2, ...) was aborteed. Call getNextException to see the cause.

Any guidance with this will be very helpful. thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.