-->
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: StaleObjectStateException
PostPosted: Fri Sep 30, 2005 11:06 am 
Newbie

Joined: Mon Aug 15, 2005 11:16 am
Posts: 14
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]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 11:19 am 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
You delete the object from the database and then the cascade tries to modify it.

Try doing the following: (I inverted two lines)

Code:
billerContacts.remove(currentContact);
ses.saveOrUpdate(billerClone);
ses.delete(currentContact);
ses.flush();
HibernateUtil.commitTransaction();


OR, Set your mapping to: cascade=all-delete-orphan and do not delete the Entity (let hibernate handle it)

Code:
<many-to-one name="entity" class="com.tsh.customerBook.model.Entity" unique="true" lazy="false" cascade="all-delete-orphan" not-null="true" not-found="ignore" >
<column name="ENTITY_OID" precision="10" scale="0" />
</many-to-one>


Let me know if this solves your problem.

Regards,
Vincent

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 2:31 pm 
Newbie

Joined: Mon Aug 15, 2005 11:16 am
Posts: 14
Thanks, but is does not solve the problem.

For first proposal: the SQL changes to, but still try to update and delete Entity
13:03:36,172 INFO [STDOUT] Hibernate: update JAVA.ENTITY set RECORD_VERSION=?, STATUS=? where ENTITY_OID=? and RECORD_VERSION=?
13:03:36,453 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=?
13:03:36,469 INFO [STDOUT] Hibernate: delete from JAVA.CONTACT where CONTACT_OID=? and RECORD_VERSION=?
13:03:36,547 INFO [STDOUT] Hibernate: delete from JAVA.PHONE_NUMBER where PHONE_NUMBER_OID=? and RECORD_VERSION=?
13:03:36,750 INFO [STDOUT] Hibernate: delete from JAVA.NAME where NAME_OID=? and RECORD_VERSION=?
13:03:36,922 INFO [STDOUT] Hibernate: delete from JAVA.EMAIL_ADDRESS where EMAIL_ADDRESS_OID=? and RECORD_VERSION=?
13:03:36,969 INFO [STDOUT] Hibernate: delete from JAVA.ENTITY where ENTITY_OID=? and RECORD_VERSION=?

The second proposal would yield the similar result. I did not delete Entity directly, I only delete the Contact and remove it from Biller's Contact collection.


SQ


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 3:18 pm 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
Are you sure that this Entity is not associated to any other object? I tend to believe that it might be associated to another contact?

First, that would explain your problem, and that would also explain why it did not delete it when you removed it from the contact collection with your mapping set to all-delete-orphans.

Please verify if this is the case.

VIncent.

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject: StaleObjectStateException
PostPosted: Fri Sep 30, 2005 4:40 pm 
Newbie

Joined: Mon Aug 15, 2005 11:16 am
Posts: 14
Definitely each Contact object has its own Entity object. I used debugger to identify all its oid.

The problem is that when update a Biller object, its related contact, and even the entity marked for deletion is to be registered for updating. When the Contact object is to be deleted, its Entity object is registered for deleting, when flushing, Hibernate somehow cannot resolve this.

I don't know how to solve this, although I can put a trigger in the database, but right now, I want to solve it since I think it might be my config or coding.

Thanks,


SQ


Top
 Profile  
 
 Post subject: StaleObjectStateException
PostPosted: Fri Sep 30, 2005 5:10 pm 
Newbie

Joined: Mon Aug 15, 2005 11:16 am
Posts: 14
To give more information. When the Entity is updated, it looks like doing nothing.

Anyone has the similar problem?

Thanks,

SQ



[size=18]When the Entity is updated:[/size]

2005-09-30 15:49:53,394 DEBUG [org.hibernate.persister.entity.BasicEntityPersister] Updating entity: [com.tsh.customerBook.model.Entity#159614]
2005-09-30 15:49:53,394 DEBUG [org.hibernate.persister.entity.BasicEntityPersister] Existing version: 1 -> New version: 1
2005-09-30 15:49:53,394 DEBUG [org.hibernate.jdbc.AbstractBatcher] about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
2005-09-30 15:49:53,394 DEBUG [org.hibernate.SQL] update JAVA.ENTITY set RECORD_VERSION=?, STATUS=? where ENTITY_OID=? and RECORD_VERSION=?
2005-09-30 15:49:53,410 DEBUG [org.hibernate.jdbc.AbstractBatcher] preparing statement
2005-09-30 15:49:53,410 DEBUG [org.hibernate.persister.entity.BasicEntityPersister] Dehydrating entity: [com.tsh.customerBook.model.Entity#159614]
2005-09-30 15:49:53,410 DEBUG [org.hibernate.type.LongType] binding '1' to parameter: 1
2005-09-30 15:49:53,410 DEBUG [org.hibernate.type.StringType] binding null to parameter: 2
2005-09-30 15:49:53,410 DEBUG [org.hibernate.type.LongType] binding '159614' to parameter: 3
2005-09-30 15:49:53,410 DEBUG [org.hibernate.type.LongType] binding '1' to parameter: 4


[size=18]
When the Entity is deleted:
[/size]

2005-09-30 15:49:53,550 DEBUG [org.hibernate.SQL] delete from JAVA.ENTITY where ENTITY_OID=? and RECORD_VERSION=?
2005-09-30 15:49:53,550 DEBUG [org.hibernate.jdbc.AbstractBatcher] preparing statement
2005-09-30 15:49:53,550 DEBUG [org.hibernate.type.LongType] binding '159614' to parameter: 1
2005-09-30 15:49:53,550 DEBUG [org.hibernate.type.LongType] binding '1' to parameter: 2
2005-09-30 15:49:53,566 DEBUG [org.hibernate.jdbc.AbstractBatcher] about to close PreparedStatement (open PreparedStatements: 1, globally: 1)

[size=18]
The Exception:
[/size]

2005-09-30 15:49:53,566 ERROR [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#159614]


Top
 Profile  
 
 Post subject: StaleObjectStateException
PostPosted: Mon Oct 03, 2005 3:34 pm 
Newbie

Joined: Mon Aug 15, 2005 11:16 am
Posts: 14
By trial and error, I modified the mapping file and finally get it working.

See the below mapping file:
Mapping file for Entity:

<class name="com.tsh.customerBook.model.Entity"
table="ENTITY"
schema="JAVA"
select-before-update="true"
dynamic-update="true"
optimistic-lock="none" >

<one-to-one name="contact"
class="com.tsh.customerBook.model.Contact"
lazy="true"
cascade="none"
constrained="false"
property-ref="entity"/>


Mapping file for Contact:

<many-to-one name="entity"
class="com.tsh.customerBook.model.Entity"
unique="true"
lazy="false"
cascade="save-update, delete"
optimistic-lock="false"
not-found="ignore" >
<column name="ENTITY_OID"
precision="10"
scale="0" />
</many-to-one>


Still the log shows both update and delete
14:25:39,627 INFO [STDOUT] Hibernate: update JAVA.ENTITY set RECORD_VERSION=? where ENTITY_OID=?
14:25:39,643 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=?
14:25:39,815 INFO [STDOUT] Hibernate: delete from JAVA.CONTACT where CONTACT_OID=? and RECORD_VERSION=?
14:25:39,831 INFO [STDOUT] Hibernate: delete from JAVA.PHONE_NUMBER where PHONE_NUMBER_OID=? and RECORD_VERSION=?
14:25:39,831 INFO [STDOUT] Hibernate: delete from JAVA.PHONE_NUMBER where PHONE_NUMBER_OID=? and RECORD_VERSION=?
14:25:39,846 INFO [STDOUT] Hibernate: delete from JAVA.PHONE_NUMBER where PHONE_NUMBER_OID=? and RECORD_VERSION=?
14:25:39,846 INFO [STDOUT] Hibernate: delete from JAVA.MAIL_ADDRESS where MAIL_ADDRESS_OID=? and RECORD_VERSION=?
14:25:39,862 INFO [STDOUT] Hibernate: delete from JAVA.NAME where NAME_OID=? and RECORD_VERSION=?
14:25:39,877 INFO [STDOUT] Hibernate: delete from JAVA.EMAIL_ADDRESS where EMAIL_ADDRESS_OID=? and RECORD_VERSION=?
14:25:39,893 INFO [STDOUT] Hibernate: delete from JAVA.ENTITY where ENTITY_OID=? and RECORD_VERSION=?


So select-before-update="true" could not be the reason that solve it. I know there are lots of posts about delete for one-to-one association. Could my case add some hints? Although I don't know what exactly solved the problem. Can Hibernate expert provide some insights?

Thanks,


SQ


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.