-->
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.  [ 2 posts ] 
Author Message
 Post subject: org.hibernate.StaleObjectStateException when adding to a set
PostPosted: Thu Nov 16, 2006 1:09 pm 
Newbie

Joined: Thu Nov 16, 2006 11:19 am
Posts: 3
For the most part I have had success creating new objects with their children but now I have issues where the Object already exists with child objects in the database and I try to add another object to the collection.

When I say child I just mean related by a foreign key.


Hibernate version: Hibernate 3 9/26/06ish

Mapping documents: see end of post

Code between sessionFactory.openSession() and session.close():
MyObject original = (MyObject) HibernateUtil.getSession().get(SubClassObject.class, myObject.getId());
original.deepCopyFrom(myObject);
HibernateUtil.getSession().save(original);

within the deepCopyFrom the issue occurs when I have an object that I add new to the originals collection.

MyObject copy = (MyObject) newObject.getClass().newInstance();
copy.deepCopyFrom(newObject);
copy.setId(null);
copy.setVersion(0);
originals.getObjectSet().add(copy);



Full stack trace of any exception that occurs:

Name and version of the database you are using:
Postgresql 8.1


The generated SQL (show_sql=true):
update my_child_objects set version=?
, active=?, child_name=?, ... where child_object_id=? and version=?


[11:57:24,147 ERROR [AbstractFlushingEventListener] Could not synchronize databas
e state with session
org.hibernate.StaleObjectStateException: Row was updated or deleted by another t
ransaction (or unsaved-value mapping was incorrect): [com.myproject.model.securi
ty.ApplicationControlModel#1000001]
at org.hibernate.persister.entity.AbstractEntityPersister.check(Abstract
EntityPersister.java:1680)
at org.hibernate.persister.entity.AbstractEntityPersister.update(Abstrac
tEntityPersister.java:2272)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert
(AbstractEntityPersister.java:2180)
at org.hibernate.persister.entity.AbstractEntityPersister.update(Abstrac
tEntityPersister.java:2450)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.ja
va:91)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)

at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)

at org.hibernate.event.def.AbstractFlushingEventListener.performExecutio
ns(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlus
hEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:993)
at com.myproject.data.hibernate.HibernateUtil.endSession(HibernateUtil.j
ava:128)
at com.myproject.ejb.common.ChainStoreCommand.execute(ChainStoreCommand.
java:81)
at com.myproject.ejb.common.CoreBean.store(CoreBean.java:133)
at sun.reflect.GeneratedMethodAccessor341.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
java:112)
at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationCo
ntextImpl.java:166)
at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3Int
erceptorsInterceptor.java:63)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
java:101)
at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invok
e(TransactionScopedEntityManagerInterceptor.java:54)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
java:101)
at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsI
nterceptor.java:47)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
java:101)
at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java
:197)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
java:101)
at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInt
erceptor.java:76)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
java:101)
at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(Stateles
sInstanceInterceptor.java:62)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
java:101)
at org.jboss.aspects.security.AuthenticationInterceptor.invoke(Authentic
ationInterceptor.java:78)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
java:101)
at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterce
ptor.java:47)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
java:101)
at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(Asynchrono
usInterceptor.java:106)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
java:101)
at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessCo
ntainer.java:225)
at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:106)
at org.jboss.aspects.remoting.AOPRemotingInvocationHandler.invoke(AOPRem
otingInvocationHandler.java:82)
at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:828)
at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:681)
at org.jboss.remoting.transport.socket.ServerThread.processInvocation(Se
rverThread.java:358)
at org.jboss.remoting.transport.socket.ServerThread.dorun(ServerThread.j
ava:412)
at org.jboss.remoting.transport.socket.ServerThread.run(ServerThread.jav
a:239)
11:57:24,147 ERROR [Log] org.hibernate.StaleObjectStateException Row was updated
or deleted by another transaction (or unsaved-value mapping was incorrect): [co
m.myproject.model.security.ApplicationControlModel#1000001]...

org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersi
ster.java:1680)
org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPers
ister.java:2272)
org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEn
tityPersister.java:2180)
org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPers
ister.java:2450)
org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:91)
org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(Abstract
FlushingEventListener.java:297)
org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListe
ner.java:27)
org.hibernate.impl.SessionImpl.flush(SessionImpl.java:993)
com.myproject.data.hibernate.HibernateUtil.endSession(HibernateUtil.java:128)
com.myproject.ejb.common.ChainStoreCommand.execute(ChainStoreCommand.java:81)
com.myproject.ejb.common.CoreBean.store(CoreBean.java:133)
sun.reflect.GeneratedMethodAccessor341.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav
a:25)
java.lang.reflect.Method.invoke(Method.java:585)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.j
ava:166)
org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsIn
terceptor.java:63)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(Transacti
onScopedEntityManagerInterceptor.java:54)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.
java:47)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:197)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.ja
va:76)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceIn
terceptor.java:62)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterc
eptor.java:78)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:4
7)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousIntercept
or.java:106)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.jav
a:225)
org.jboss.aop.Dispatcher.invoke(Dispatcher.java:106)
org.jboss.aspects.remoting.AOPRemotingInvocationHandler.invoke(AOPRemotingInvoca
tionHandler.java:82)
org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:828)
org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:681)
org.jboss.remoting.transport.socket.ServerThread.processInvocation(ServerThread.
java:358)
org.jboss.remoting.transport.socket.ServerThread.dorun(ServerThread.java:412)
org.jboss.remoting.transport.socket.ServerThread.run(ServerThread.java:239)


(Parent) MyObject.hbxml

<hibernate-mapping>

<class name="com.myproject.model.security.MyObject"
table="my_objects" dynamic-update="true">

<id name="id" column="my_object_id" type="long">
<generator class="sequence">
<param name="sequence">my_object_sequence</param>
</generator>
</id>
<version name="version"></version>


<property name="cols" column="cols"></property>

<property name="title" column="title"></property>
...snip...

<set name="children" cascade="save-update" lazy="false" fetch="join">
<key column="my_object_id" not-null="true" />
<one-to-many class="com.myproject.model.security.MyChildObject"/>
</set>

</class>

</hibernate-mapping>



(Related) MyChildObject

<hibernate-mapping>

<class name="com.myproject.model.security.ApplicationControlModel"
table="my_child_objects" dynamic-update="true">

<id name="id" column="my_child_object_id" type="long">
<generator class="sequence">
<param name="sequence">my_child_objects_sequence</param>
</generator>
</id>
<version name="version"></version>

<property name="active" column="active"></property>

<property name="child_name" column="child_name"></property>

<property name="my_object_id" column="my_object_id" insert="false" update="false"></property>

...snip... there were no granchildren in this case

<set name="grand_children" cascade="save-update" lazy="false" fetch="join">
<key column="my_child_object_id" not-null="true" />
<one-to-many class="com.myproject.model.security.MyChildObject"/>
</set>

</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 17, 2006 1:16 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
You could either do

Code:
copy.setVersion(null);


or specify the unsaved value for <version> as 0.


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