StaleObjectStateError.....
Okay, I have added my hibernate mapping below..and more explanation on this issue. I really need help with this...
I have an object lets say its called House. This object has a many to one relationship with another object called Father :).
This relationship is bi-directional. The id for Father is auto-generated using GUID. I am calling a DAO object from a session bean where the transaction is container managed and set to required.
In the web tier, I get a reference to the House object, update some fields and either add a Father, update the father or remove the Father (house.setFather(null)).
Then, I make a call to one method on a House DAO called updateHouse that handles all these calls in the following way:
updateHouse(House h){
if(h.getFather()!=null){
//we have a father
if(h.getFather().getFatherId()!=null){
//this father is being updated
session.update(h.getFather());
}else{
//this father is being created for the House
session.save(h.getFather();
}
}
//The house has other properties that could also be updated
session.update(h);
}
Now when I run this code, I get a StaleObjectStateError on the Father object telling me the following:
Problem updating House: my.package.House@2c14f9[houseId=1] and the Exception Msg is:
my.package.util.DataAccessException: net.sf.hibernate.StaleObjectStateException: Row
was updated or deleted by another transaction (or unsaved-value mapping was incorrect) for my.package.Father instance with identifier: 402880e5f805977600f807d5f42b0004
I am using Hibernate 2.0.2 and following are my mapping files:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by Middlegen Hibernate plugin
http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
<class
name="my.package.House"
table="SCHOOL"
proxy="my.package.House"
dynamic-update="true"
>
<meta attribute="implements">net.sf.hibernate.Lifecycle</meta>
<meta attribute="implements">net.sf.hibernate.Validatable</meta>
<id
name="houseId"
type="java.lang.Integer"
column="HOUSE_ID"
unsaved-value="null"
>
<generator class="assigned" />
</id>
<timestamp
column="user_dt"
name="userDt"
/>
<property
name="userId"
type="java.lang.String"
column="user_id"
length="8"
/>
<!-- associations -->
<!--one-to-one association to Teacher -->
<many-to-one
name="principal"
class="my.package.Father"
not-null="false"
>
<column name="principal_id" />
</many-to-one>
</class>
<class
name="my.package.Father"
table="TEACHER"
proxy="my.package.Father"
dynamic-update="true"
>
<meta attribute="implements">net.sf.hibernate.Lifecycle</meta>
<meta attribute="implements">net.sf.hibernate.Validatable</meta>
<id
name="fatherId"
type="java.lang.String"
column="FATHER_ID"
unsaved-value="none"
>
<generator class="uuid.hex" />
</id>
<property
name="name"
type="java.lang.String"
column="name"
not-null="false"
length="100"
/>
<property
name="comment"
type="java.lang.String"
column="comment"
not-null="false"
length="3500"
/>
</hibernate-mapping>
My classes were auto-generated by hbm2java. This has taken all weekend and I am not sure why Hibernate is doing this..