I try to delete a Contact object from Biller object's Contact collection. The Contact object has an Entity object which in turn contains collection of PhoneNumber, ect.
After I removed the Contact object from Biller's Contact collection and deleted it. I update the Biller object. During session flush, a StaleObjectStateException is thrown. Look at the SQL log, Hibernate is first updating the Biller and Entity, then deleting the Contact object during which the exception is thrown.
I set the cascade for those object as shown below.
Thanks very much for your help, I guess the order of delete and update is important in this case, I may have made mistakes here.
SQ
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
[b]Hibernate version:[/b]
Hibernate 3.0.5 with JBoss 4.0.2
[b]Mapping documents:[/b]
Part of Mapping file of Biller:
<set name="contacts"
inverse="true"
lazy="true"
cascade="save-update"
batch-size="10"
fetch="subselect" >
<key>
<column name="BILLER_OID"
precision="10"
scale="0" />
</key>
<one-to-many class="com.tsh.customerBook.model.Contact"
not-found="ignore" />
</set>
Part of Mapping file for Contact:
<many-to-one name="entity"
class="com.tsh.customerBook.model.Entity"
unique="true"
lazy="false"
cascade="save-update, delete"
not-null="true"
not-found="ignore" >
<column name="ENTITY_OID"
precision="10"
scale="0" />
</many-to-one>
<many-to-one name="biller"
class="com.tsh.customerBook.model.Biller"
lazy="proxy"
cascade="none"
not-found="ignore" >
<column name="BILLER_OID"
precision="10"
scale="0" />
</many-to-one>
Part of Mapping file for Entity:
<one-to-one name="contact"
class="com.tsh.customerBook.model.Contact"
lazy="false"
cascade="none"
property-ref="entity"/>
<set name="phoneNumbers"
inverse="true"
lazy="true"
cascade="save-update, delete"
batch-size="3"
fetch="subselect">
<key>
<column name="ENTITY_OID"
precision="10"
scale="0" />
</key>
<one-to-many class="com.tsh.customerBook.model.PhoneNumber"
not-found="ignore" />
</set>
[b]Code between sessionFactory.openSession() and session.close():[/b]
Biller billerClone = (Biller) ses.load(Biller.class, biller.getBillerOid());
Set<Contact> billerContacts = billerClone.getContacts();
billerContacts.remove(currentContact);
ses.delete(currentContact);
ses.saveOrUpdate(billerClone);
ses.flush();
HibernateUtil.commitTransaction();
[b]Full stack trace of any exception that occurs:[/b]
[org.hibernate.event.def.AbstractFlushingEventListener] Could not synchronize database state with session
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.tsh.customerBook.model.Entity#15556]
at org.hibernate.persister.entity.BasicEntityPersister.check(BasicEntityPersister.java:1441)
at org.hibernate.persister.entity.BasicEntityPersister.delete(BasicEntityPersister.java:2072)
at org.hibernate.persister.entity.BasicEntityPersister.delete(BasicEntityPersister.java:2213)
at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:59)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at com.tsh.customerBook.servlet.editCustomer.EditCustomerContactInfoServlet.deleteContactInformation(EditCustomerContactInfoServlet.java:681)
[b]Name and version of the database you are using:[/b]
Oracle 9i
[b]The generated SQL (show_sql=true):[/b]
2005-09-30 09:16:47,074 INFO [STDOUT] Hibernate: update JAVA.BILLER set RECORD_VERSION=?, BILLER_CONTROLS_OID=?, ENTITY_OID=?, STATUS=?, MODIFIED_DATE=?, MODIFIED_TIMEZONE=? where BILLER_OID=? and RECORD_VERSION=?
2005-09-30 09:16:47,105 INFO [STDOUT] Hibernate: update JAVA.ENTITY set RECORD_VERSION=?, STATUS=? where ENTITY_OID=? and RECORD_VERSION=?
2005-09-30 09:16:47,121 INFO [STDOUT] Hibernate: delete from JAVA.CONTACT where CONTACT_OID=? and RECORD_VERSION=?
2005-09-30 09:16:47,137 INFO [STDOUT] Hibernate: delete from JAVA.PHONE_NUMBER where PHONE_NUMBER_OID=? and RECORD_VERSION=?
2005-09-30 09:16:47,230 INFO [STDOUT] Hibernate: delete from JAVA.ENTITY where ENTITY_OID=? and RECORD_VERSION=?
[b]Debug level Hibernate log excerpt:[/b]