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.  [ 7 posts ] 
Author Message
 Post subject: Dirty objects and class="assigned" problem
PostPosted: Mon Oct 18, 2004 2:51 pm 
Regular
Regular

Joined: Fri Oct 08, 2004 4:11 am
Posts: 78
Location: Nottingham, England
Hi,
I have a parent/child one to many association which is not behaving as I expect. Both maps have assigned keys with a version tag to aide in determining whether the object is new or old. child objects are added to a set in the parent class. So what I am expecting to happen is that when i add a child it does not overwrite what is already in the set which is what it is doing.

So it is behaving like an object whose id/unsave-value is unknown and thus overitting what is already in the set. Why is this happening??

Note: The objects are just being added to the Set as a user adds an item. The idea, once the user has added all items, is to save the order/orderitem objects in one hit.

many thanks in Advance

Andrew

Hibernate code:
Order neworder = new order()
if (neworder == null) {
try {
neworder.setID(random.nextInt()); //if map has an assigned id
}catch(e) {
//do some task
}
}

try {

OrderItem item = new OrderItem();

//read order
var orderID_temp = cocoon.request.get("orderID")

//1. Check if stock code exists.
var newStk = stk.checkStock(cocoon.request.get("stockID"));

var amount = cocoon.request.get("quantity");

//2. Add a stock item to the order.

item.setID(random.nextInt()); //assigned id
item.setStock(newStk);
item.setAmount(amount);
item.setPrice(newStk.getUnitCost() * amount);
item.setArtistName(artist_name);


neworder.setPriceTotal(neworder.getPriceTotal() + (newStk.getUnitCost() * amount))
neworder.setUserID(parseInt(userGlobal.getID()));

neworder.setOrderItems(new HashSet());
neworder.getOrderItems().add(item);
item.setOrder(neworder);


}catch(e) {
}

[b/]

[b]Hibernate version:

2.1.1


Mapping documents:

Order.hbm.xml

<class name="Order" table="orderstbl">

<id name="ID" column="order_id">
<generator class="assigned"/>
</id>

<version name="Version"
column="version_id"
unsaved-value="0"/>

<property name="PriceTotal" column="price_total" type="double" not-null="true"/>
<property name="OrderDate" column="order_date" type="timestamp" not-null="true"/>
<property name="UserID" column="user_id" type="integer" not-null="true"/>

<set
name="OrderItems"
table="orderitemstbl"
inverse="true"
cascade="all-delete-orphan">

<key column="order_id"/>
<one-to-many class="OrderItem"/>

</set>

</class>

OrderItems.hbm.xml

<class name="OrderItem" table="orderitemstbl">

<id name="ID" column="orderitem_id">
<generator class="assigned"/>
</id>

<version name="Version"
column="version_id"
unsaved-value="0"/>

<property name="OrderID" column="order_id" type="string" not-null="true" insert="false" update="false"/>
<property name="StockID" column="stock_id" type="string" not-null="true" insert="false" update="false"/>
<property name="Amount" column="amount" type="integer"/>
<property name="Price" column="price" type="double"/>

<many-to-one name="Order" class="Order" column="order_id" />
<many-to-one name="Stock" class="Stock" column="stock_id"/>

</class>


Code between sessionFactory.openSession() and session.close():

Order.java

transaction = session.beginTransaction();

session.save(myOrder);

transaction.commit();



Name and version of the database you are using:
PostgreSQL Version 7.4.1


Top
 Profile  
 
 Post subject: Just to add...
PostPosted: Mon Oct 18, 2004 5:00 pm 
Regular
Regular

Joined: Fri Oct 08, 2004 4:11 am
Posts: 78
Location: Nottingham, England
sorry if it seems like i'm you guys mundane questions, but i'd just like to know that i'm heading in the right direction with all this stuff or over a cliff edge as the case may be.

regards in advance


Andrew

P.S I have the book (Hibernate in Action) before you suggest it ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 19, 2004 2:08 pm 
Newbie

Joined: Mon Aug 30, 2004 1:37 am
Posts: 16
Andrew,
I am new to Hibernate myself - but since nobody else has replied I do have one suggestion..
There are a few typos in HIA in the area covering using version with assigned keys on page 332. The
Code:
unsaved-value="0"
is not correct - -you need to change it to
Code:
unsaved-value="negative"
in your Order.hbm.xml and OrderItems.hbm.xml. Then in your Order.java and OrderItems.java you need to set the initial value for version to a negative value:
Code:
private int version = -1;

The latest errata for HIA is now at:
http://forum.hibernate.org/viewtopic.php?t=935347
If you are using ant - I have found it helpful to validate my mapping files by using the following task:
Code:
      <target name="validate-xml" description="validate the xml mapping files" >
         <xmlvalidate failonerror="no" lenient="no" warn="yes">
            <fileset dir="${mapping.dir}" includes="*.hbm.xml" />
         </xmlvalidate>
      </target>

I hope that this helps. Good luck.
Grainne.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 19, 2004 2:49 pm 
Regular
Regular

Joined: Fri Oct 08, 2004 4:11 am
Posts: 78
Location: Nottingham, England
Hi grainne,
thanks for replying my message. I was begining to think this forum was only for people who already knew everything about hibernate! Anyway, I applied the changes you suggested, I am surprised that the information you mention is so incorrect, but i am still getting the same error message:

19:37:17,070 DEBUG SessionImpl:528 - opened session
19:37:17,074 DEBUG JDBCTransaction:37 - begin
19:37:17,077 DEBUG JDBCTransaction:41 - current autocommit status:false
19:37:17,079 DEBUG Cascades:408 - version unsaved-value strategy NEGATIVE
19:37:17,081 DEBUG SessionImpl:1326 - saveOrUpdate() previously saved instance with id: 123456
19:37:17,083 DEBUG SessionImpl:1374 - updating [test.Order#123456]
19:37:17,093 DEBUG SessionImpl:1221 - collection dereferenced while transient [test.Order.OrderItems#123456]
19:37:17,101 DEBUG Cascades:497 - processing cascades for: test.Order
19:37:17,103 DEBUG Cascades:524 - cascading to collection: test.Order.OrderItems
19:37:17,106 DEBUG Cascades:113 - cascading to saveOrUpdate()
19:37:17,108 DEBUG Cascades:408 - version unsaved-value strategy NEGATIVE
19:37:17,110 DEBUG SessionImpl:1321 - saveOrUpdate() unsaved instance
19:37:17,113 DEBUG SessionImpl:786 - saving [test.OrderItem#654321]
19:37:17,115 DEBUG Cascades:312 - id unsaved-value: id_value
19:37:17,124 DEBUG Cascades:506 - done processing cascades for: test.Order
19:37:17,126 DEBUG JDBCTransaction:59 - commit
19:37:17,128 DEBUG SessionImpl:2193 - flushing session
19:37:17,130 DEBUG Cascades:497 - processing cascades for: test.Order
19:37:17,132 DEBUG Cascades:524 - cascading to collection: test.Order.OrderItems
19:37:17,134 DEBUG Cascades:113 - cascading to saveOrUpdate()
19:37:17,137 DEBUG SessionImpl:1306 - saveOrUpdate() persistent instance
19:37:17,139 DEBUG Cascades:506 - done processing cascades for: test.Order
19:37:17,140 DEBUG SessionImpl:2321 - Flushing entities and processing referenced collections
19:37:17,143 DEBUG WrapVisitor:76 - Wrapped collection in role: test.Order.OrderItems
19:37:17,146 DEBUG SessionImpl:2415 - Updating entity: [test.Order#123456]
19:37:17,148 DEBUG Versioning:26 - Incrementing: 0 to 1
19:37:17,154 DEBUG SessionImpl:2768 - Collection found: [test.Order.OrderItems#123456], was: [<unreferenced>]
19:37:17,156 DEBUG SessionImpl:2664 - Processing unreferenced collections
19:37:17,158 DEBUG SessionImpl:2678 - Scheduling collection removes/(re)creates/updates
19:37:17,174 DEBUG SessionImpl:2217 - Flushed: 1 insertions, 1 updates, 0 deletions to 2 objects
19:37:17,176 DEBUG SessionImpl:2222 - Flushed: 1 (re)creations, 0 updates, 1 removals to 1 collections
19:37:17,179 DEBUG Printer:75 - listing entities:
19:37:17,182 DEBUG Printer:82 - test.Order{OrderDate=19 October 2004 19:37:09, PriceTotal=4.99, UserID=1, OrderItems=[OrderItem#654321], version=1, ID=123456}
19:37:17,184 DEBUG Printer:82 - test.OrderItem{Stock=Stock#d1e22501fbf2ece300fbf2efe1440001, Amount=1, OrderID=123456, Price=4.99, Order=Order#123456, StockID=d1e22501fbf2ece300fbf2efe1440001, version=-1, ID=654321}
19:37:17,186 DEBUG SessionImpl:2258 - executing flush
19:37:17,188 DEBUG EntityPersister:454 - Inserting entity: [test.OrderItem#654321]
19:37:17,191 DEBUG EntityPersister:455 - Version: -1
19:37:17,193 DEBUG BatcherImpl:192 - about to open: 0 open PreparedStatements, 0 open ResultSets
19:37:17,195 DEBUG SQL:223 - insert into orderitemstbl (version, amount, price, order_id, stock_id, orderitem_id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into orderitemstbl (version, amount, price, order_id, stock_id, orderitem_id) values (?, ?, ?, ?, ?, ?)
19:37:17,197 DEBUG BatcherImpl:227 - preparing statement
19:37:17,200 DEBUG EntityPersister:389 - Dehydrating entity: [test.OrderItem#654321]
19:37:17,203 DEBUG Cascades:312 - id unsaved-value: id_value
19:37:17,205 DEBUG BatcherImpl:28 - Adding to batch
19:37:17,207 DEBUG BatcherImpl:50 - Executing batch size: 1
19:37:17,259 DEBUG BatcherImpl:199 - done closing: 0 open PreparedStatements, 0 open ResultSets
19:37:17,261 DEBUG BatcherImpl:240 - closing statement
19:37:17,263 DEBUG EntityPersister:631 - Updating entity: [test.Order#123456]
19:37:17,265 DEBUG EntityPersister:632 - Existing version: 0 -> New version: 1
19:37:17,268 DEBUG BatcherImpl:192 - about to open: 0 open PreparedStatements, 0 open ResultSets
19:37:17,270 DEBUG SQL:223 - update orderstbl set version=?, price_total=?, order_date=?, user_id=? where order_id=? and version=?
Hibernate: update orderstbl set version=?, price_total=?, order_date=?, user_id=? where order_id=? and version=?
19:37:17,273 DEBUG BatcherImpl:227 - preparing statement
19:37:17,275 DEBUG EntityPersister:389 - Dehydrating entity: [test.Order#123456]
19:37:17,326 ERROR StaleObjectStateException:27 - An operation failed due to stale data
net.sf.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) for test.Order instance with identifier: 123456

The problem is the many-to-one Set association (OrderItems) being updated which is dirty which is causing the trouble. From the Hibernate manual, I understand what is doing, what I want to do is find a solution for this. Any ideas anybody???

thanks in advance


Andrew


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 19, 2004 2:54 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Quote:
So what I am expecting to happen is that when i add a child it does not overwrite what is already in the set which is what it is doing.


what happens if you don't use hibernate? i mean what happens if you add two objects in a set? how can the set determine wether the object is or not in the set?

I'm asking that because i'm not sure to understand where the problem is exactly, this may (or not) help:
http://www.hibernate.org/116.html#A11

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 19, 2004 3:16 pm 
Regular
Regular

Joined: Fri Oct 08, 2004 4:11 am
Posts: 78
Location: Nottingham, England
Anthony,
the problem is the Order.Orderitems collection is dirty. I know this because i added the following , mutable="false", to my class and got the following error:

'java.lang.RuntimeException: Exception: reassociated object has dirty collection reference'.

When I take the mutable reference out I get:

'Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) for test.Order instance with identifier: 123456'

The two messages are pointing to the same problem, the collection is dirty because I have added an item to it!!! Why!!????

In the Hibernate In Action book page 171 it says:

'..........Hibernate will increment the version number whenever an object is dirty. This includes all dirty properties, whether they


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 20, 2004 2:44 am 
Regular
Regular

Joined: Fri Oct 08, 2004 4:11 am
Posts: 78
Location: Nottingham, England
For anybody interested....
I managed to resolve this issue by removing the bi-directional association between Order and OrderItem created by the many-to-one line in the OrderItem map.
I don't have clue why removing these lines would cure the problem, maybe someone more experienced in hibernate issues can take up the reigns here!!!


Andrew


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