-->
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: Newbie Question: Nest many-to-one inside composite-element?
PostPosted: Thu Jul 24, 2008 1:45 pm 
Newbie

Joined: Thu Jul 24, 2008 1:23 pm
Posts: 16
Hi All,

I have a newbie question.

I have an Order class, which contains a set of OrderItems. I am persisting the OrderItems as a set of composite-elements, stored in a table called ORDER_ITEM. Each OrderItem has a relationship to an Item object. Can I nest within each orderItem a unique many-to-one relationship with an Item entity? In order words, I want to persist the Order and related OrderItems without saving any Items.

The mappings are as follows:

Order.hbm.xml:
<class name="poc.domain.Order" schema="HIBERNATE" table="ORDER">

<id name="id" type="long" column="ID">
<generator class="native"/>
</id>

<property name="orderDate" type="date" column="ORDER_DATE" />
<property name="total" type="double" column="TOTAL" />

<set name="orderItems" schema="HIBERNATE" table="ORDER_ITEM">
<key column="ORDER_ID" />
<composite-element class="poc.domain.OrderItem">
<property name="count" type="integer" column="ITEM_COUNT" />
<many-to-one name="item" class="poc.domain.Item" column="ITEM_ID"
unique="true" update="false" insert="false" />
</composite-element>
</set>

</class>

Item.hbm.xml:
<class name="poc.domain.Item" schema="HIBERNATE" table="ITEM">

<id name="id" type="long" column="ID">
<generator class="native"/>
</id>

<property name="name" type="string" column="NAME" />
<property name="description" type="string" column="DESCRIPTION" />
<property name="price" type="double" column="PRICE" />

</class>

I am getting the following error, which I guess means I can't do the nesting but I wanted to make sure:

[object references an unsaved transient instance - save the transient instance before flushing: poc.domain.Item]: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: poc.domain.Item


Thanks!
Steve


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 24, 2008 3:13 pm 
Newbie

Joined: Thu Mar 17, 2005 2:06 pm
Posts: 12
Steve,

When you saving Order, the set contains at least one Item that hasn't been saved. You must save all the Item's in your Order before you can save order.

The other option would be to update your hbm.xml configuration to save items in the set.


Top
 Profile  
 
 Post subject: Newbie Question: Nest many-to-one inside composite-element?
PostPosted: Thu Jul 24, 2008 3:30 pm 
Newbie

Joined: Thu Jul 24, 2008 1:23 pm
Posts: 16
Hi live216,

Thanks, I think I now know what the problem is, I just don't know how to solve it. The Item objects already exist in a table in the database. I just want to 'attach' references to them to an Order via the Order_Item table. I think I need to pre-load the items into the Hibernate session so their Ids are available but I'm not sure how to do this.

Thanks,
Steve


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 24, 2008 4:13 pm 
Newbie

Joined: Thu Jul 24, 2008 1:23 pm
Posts: 16
Ok, figured it out...forgot to load in the Item objects before trying to use them -- Doh!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 25, 2008 4:07 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Sometimes the simplest of errors are the toughest to find and fix.

Glad you fixed it!

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 25, 2008 9:06 am 
Newbie

Joined: Thu Jul 24, 2008 1:23 pm
Posts: 16
Thanks! I guess I was working with 'transient' objects rather than 'persistent' ones.


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.