-->
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: one-to-many insert
PostPosted: Fri May 27, 2005 7:51 am 
Newbie

Joined: Fri May 27, 2005 7:40 am
Posts: 2
I have the mapping of my object like the following. When i try to Session.save(order) Order is getting inserted but Order Items are updated. How do we change it to inserting ?

<hibernate-mapping>
<class name="ps.model.Order" table="Order">
<id type="java.lang.Long" column="Order_ID" name="OrderId">
<generator class="assigned"/>
</id>
<property type="java.util.Date" column="DATE_CREATED" name="dateCreated" />
<property type="java.util.Date" column="TIME_CREATED" name="timeCreated" />
<property type="java.lang.String" column="CREATED_BY" name="createdBy" />
<property type="java.lang.String" column="Order_TYPE" name="OrderType" />
<set name="items" lazy="false" cascade="save-update" inverse="true">
<key column="Order_ID" />
<one-to-many class="ps.model.OrderItems" />
</set>

</class>
<class name="ps.model.OrderItems" table="Order_ITEMS">
<composite-id>
<key-many-to-one name="OrderId" class="ps.model.Order" column="OrderId"/>
<key-property name="itemNo" type="java.lang.Long">
<column name="ITEM_NO" not-null="true"/>
</key-property>
</composite-id>
<property type="java.lang.String" column="STATUS" name="status" />
<property type="java.lang.Long" column="QTY" name="qty" />

</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 27, 2005 9:51 am 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
Look at the cascade attribute on your one-to-many association. You have not specified it.

Read chapter 11.11

http://www.hibernate.org/hib_docs/v3/re ... transitive

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 28, 2005 4:02 am 
Newbie

Joined: Fri May 27, 2005 7:40 am
Posts: 2
I have added the cascade with the set.

<set name="items" lazy="false" cascade="save-update" inverse="true">

do we need to add anywhere else ?!!


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 30, 2005 7:12 pm 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
I am probably not getting your question right.

Maybe your problem is: You are creating a new Order and associate OrderItems to it and you would like Hibernate to create new OrderItems instead of issuing an Update statement.

Is that correct?

If this is the case, you must understand that an OrderItem is unique (Two java objects represent the same OrderItem (id) are the same object, in terms of persistence).

If you associate the same reference of an OrderItem to multiple Orders, or associate different instances representing the same OrderItem, the OrderItem will be the same for all of them and it will get updated each time you modify one of them.

If you want new OrderItems to be associated to each Order, do not reuse existing OrderItems, just create new ones.

Good luck

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 31, 2005 12:05 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
<hibernate-mapping>
<class name="ps.model.Order" table="Order">
<id type="java.lang.Long" column="Order_ID" name="OrderId">
<generator class="assigned"/>
</id>
<property type="java.util.Date" column="DATE_CREATED" name="dateCreated" />
<property type="java.util.Date" column="TIME_CREATED" name="timeCreated" />
<property type="java.lang.String" column="CREATED_BY" name="createdBy" />
<property type="java.lang.String" column="Order_TYPE" name="OrderType" />

//try this

<bag name="items" lazy="true" inverse="true" cascade="all">
<key column="Order_ID"/>
<one-to-many class="ps.model.OrderItems"/>
</bag>


</class>
<class name="ps.model.OrderItems" table="Order_ITEMS">
<composite-id>
<key-many-to-one name="OrderId" class="ps.model.Order" column="OrderId"/>
<key-property name="itemNo" type="java.lang.Long">
<column name="ITEM_NO" not-null="true"/>
</key-property>
</composite-id>
<property type="java.lang.String" column="STATUS" name="status" />
<property type="java.lang.Long" column="QTY" name="qty" />

</class>
</hibernate-mapping>

Hi
Try this and the other point is
<key-many-to-one name="OrderId" class="ps.model.Order" column="OrderId"/>

why do have to give it in the composite id?

Its is not necessary.
will your item_no get repeated?





[/b]

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


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.