Hi,
I get a StaleObjectStateException when I try to save a bi-directional relationship using hibernate. I read the Item/Bid example in Hibernate in Action, and I cut-and-paste that part of the mapping in my code, but I still get this StaleObjectStateException.
Can someone please tell me what did I miss? I really appreciate any help.
Code:
WARNING: 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 db.model.Cookie instance with identifier: 0
at net.sf.hibernate.persister.AbstractEntityPersister.check(AbstractEntityPersister.java:513)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:664)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:621)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2449)
Code:
UserDAO userDAO= new UserDAO();
User user = new User();
user.setUserName("abc");
Data data = new Data();
data.setUser(user);
user.getAllData().add(data);
userDAO.makePersistent(user);
Code:
<class name="User" table="USER" lazy="true">
<cache usage="read-write" />
<id name="id" type="long" column="ITEM_ID"
unsaved-value="null" access="db.DirectSetAccessor">
<generator class="native" />
</id>
<bag name="allData"
cascade="all"
inverse="true"
lazy="true"
access="db.DirectSetAccessor">
<key>
<column name="ITEM_ID" not-null="true"/>
</key>
<one-to-many class="Data"/>
</bag>
</class>
Code:
<class name="Data" table="DATA" lazy="true">
<many-to-one
name="user"
class="User"
column="ITEM_ID"
update="false"
cascade="none"
not-null="true"
outer-join="false"
access="db.DirectSetAccessor"
foreign-key="FK1_ITEM_ID"/>
</class>