-->
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.  [ 8 posts ] 
Author Message
 Post subject: Failed to save child object in one-to-many
PostPosted: Thu Jun 24, 2004 6:46 pm 
Newbie

Joined: Thu Jun 24, 2004 6:09 pm
Posts: 9
I am running into exception when I use following one-to-many mapping using Hibernate. I need favor in this one-to-many mapping when the child has composite key in it.

My java classes are as follows:

public class Order
{
int orderId; //PK
Set orderLineItem;
......
}

public java OrderLineItemPK
{
int lineNo;
int orderId;
}

public java OrderLineItem
{
OrderLineItemPK pk;
String desc;
......
}

In the OrderLineItem class OrderLineItemPK will be the composite key and for this purpose I am using OrderLineItemPK.

Also the relationship between Order and OrderLineItem is one to many and unidirectional.

My mapping files are as follows:

Order.hbm.xml
==========

<hibernate-mapping>
<class
name="com.XXX.Order" table="Order">
<id name="orderId" type="int" unsaved-value="null">
<column name="ORDER_ID" sql-type="number(10)" not-null="true"/>
<generator class="increment"></generator>
</id>

<set name="theBookingLineItems"
table="ORDER_LINE_ITEM"
lazy="false"
inverse="false"
cascade="all"
sort="unsorted">
<key
column="ORDER_ID"
/>
<one-to-many
class="com.XXX.OrderLineItem"
/>
</set>
</hibernate-mapping>

OrderLineItem.hbm.xml
===============

<hibernate-mapping>
<class name="com.XXX.OrderLineItem" table="ORDER_LINE_ITEM">
<composite-id
name="orderLineItemPK"
class="com.xxx.OrderLineItemPK" >
<key-property name="orderId" column="ORDER_ID" type="integer" />
<key-property name="lineNo" column="LI_NUM" type="integer"/>
</composite-id>

</hibernate-mapping>


Now in my java program I call save() on the order object as follows:

//######
sess.save(order);
sess.flush();
//######

But it just saves the Order record in the table. The Order line item never saves.

Can some one help me in handling this kind of situation where I use composite key in the child object? Issue here may be I am using one of the fields of the composite key in the mapping of Set of the Parent object.

FYI: I am using hibernate-2.1 version.

Thank in advance. ANy help will be appreciated.

regards,
Naga


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 25, 2004 6:54 am 
Newbie

Joined: Fri May 14, 2004 1:33 pm
Posts: 4
Read this, it may help you

http://www.hibernate.org/hib_docs/refer ... child.html


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 25, 2004 9:02 am 
Newbie

Joined: Thu Jun 24, 2004 6:09 pm
Posts: 9
Here the issue is dealing with PK fields of the child. If some one can give pointers about that, it will be great.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 25, 2004 12:10 pm 
Newbie

Joined: Thu Jun 24, 2004 6:09 pm
Posts: 9
Still it is failing..............


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 1:36 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
use inverse=true on one-to-many and <key-many-to-one> in the PK declaration

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 3:23 pm 
Beginner
Beginner

Joined: Mon Nov 03, 2003 11:48 pm
Posts: 29
emmanual,
Do you mean put the key many-to-one in the parent class, or the child class?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 3:55 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
replace <key-property by <key-many-to-one

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 01, 2008 10:55 am 
Beginner
Beginner

Joined: Tue Jan 08, 2008 2:15 pm
Posts: 22
What is the equivalent of <key-many-to-one> with annotations?


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