Hello there,
I am really stuck!
I have to classes an Order that contains a Collection (Set) of OrderItems. The ids are taken care off via
sequence (PSQL).
If I add one OrderItem to the Order and save the Order all is fine. However, if I add two or more OrderItems to the Order I get a:
Code:
net.sf.hibernate.HibernateException: Another object was associated with this id (the object with the given id was already loaded): [OrderItem#0]
Code:
Order order = new Order( );
order.add( new OrderItem() );
sess.save( order );
is ok.
Code:
Order order = new Order( );
order.add( new OrderItem() );
order.add( new OrderItem() );
sess.save( order );
is not ok????
Surely Hibernate must be able to distingish the two newly created objects.
I am fully aware of the limitation that one can not have two loaded objects pointing to the same DB row, but in this case none of the objects do exist? (
http://hibernate.bluemars.net/117.html#A19)
Please help! I do not understand this. Is there an easy explanation or solution. What am I missing?
Thanks Bernie
Code:
public class Order
{
private long _id;
private Set _items = new HashSet( );
public void add( OrderItem item)
{
_items.add( item );
}
...
}
public class OrderItem
{
private long _id;
...
}
<hibernate-mapping>
<class
name="OrderItem"
table="T_ORDER_ITEM"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="id"
column="id"
type="long"
>
<generator class="sequence">
</generator>
</id>
...
<hibernate-mapping>
<class
name="Order"
table="T_ORDER"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="id"
column="id"
type="long"
>
<generator class="sequence">
</generator>
</id>
<set
name="items"
lazy="true"
inverse="false"
cascade="all"
sort="unsorted"
>
<key
column="order_fk"
/>
<one-to-many
class="OrderItem"
/>
</set>
...