-->
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.  [ 4 posts ] 
Author Message
 Post subject: updating between transactions
PostPosted: Thu Mar 09, 2006 3:29 am 
Newbie

Joined: Sat Feb 25, 2006 8:19 am
Posts: 7
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0.5

Name and version of the database you are using: oracle10g

I am having trouble updating an object outside of a session. I thought that the following case makes perfect sense:
session 1:
save an object from to the database

outside of the session:
update the object, set some properties

session 2:
re-attach the object, and update it (using update() or saveOrUpdate())

well, for some strange reason this doesn't work for me at all. I tried using a second approach:

session 1:
get an object from to the database

session 2:
update the object, set some properties and then call update()

this too failed on me. I then tried a 3rd approach, which is quite silly, but I thought I'd give it a try

session 1:
save an object to the database

session 2:
get the same object again using it's pk (even though the original object is still in my hands)
update the object, set some properties and then call update()

surprise surprise - it fails!

where can I be doing something wrong? what might cause it? I keep getting

08/03/2006 14:23:57 org.hibernate.event.def.AbstractFlushingEventListener performExecutions
SEVERE: 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.taldor.ko.datamodel.vo.Site#916]
at org.hibernate.persister.entity.BasicEntityPersister.check(BasicEntityPersister.java:1441)
at org.hibernate.persister.entity.BasicEntityPersister.update(BasicEntityPersister.java:1986)
at org.hibernate.persister.entity.BasicEntityPersister.updateOrInsert(BasicEntityPersister.java:1909)
at org.hibernate.persister.entity.BasicEntityPersister.update(BasicEntityPersister.java:2149)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:75)
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:137)
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 org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)

after running this SQL command:
Hibernate: update SITE set UPDATE_TIME=?, SITE_CODE=?, SITE_TYPE=?, REGION=?, WORK_MODE=?, DEFAULT_CODEC=?, VERSION=?, CREATION_TIME=? where SITE_ID=? and UPDATE_TIME=?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 09, 2006 4:22 am 
Newbie

Joined: Thu Oct 14, 2004 10:05 am
Posts: 9
Location: Copenhagen, Denmark
What is the value of the UPDATE_TIME field in the database and in your program, respectively?
How is it assigned a vlaue on insert?
What does the mapping file and the DDL look like?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 09, 2006 4:59 am 
Newbie

Joined: Sat Feb 25, 2006 8:19 am
Posts: 7
At first I noticed that the update method was using setUpdateTime to change it in the code. Of course it isn't right, so I removed this line, and had Hibernate change it when he saves or updates a row. well, deleting the line didn't get it to work.
now, when I put a breakpoint on the tx.commit() line, I compare the object's updateTime field, with the table's UPDATE_TIME - the are identical. However, I am thinking now that maybe Hibernate sees a different UPDATE_TIME in the database, after updating the row for some reason, while in the same transaction - that is why I might not be seeing what Hibernate sees in the database.

The mapping file for the Site class contains the following lines:

Code:
<hibernate-mapping>
   <class name="example.Site" table="SITE">
      <meta attribute="class-description" inherit="false">@hibernate.class table="SITE"</meta>
      <id name="pk" type="java.lang.Integer" column="SITE_ID">
         <generator class="sequence">
            <param name="sequence">SITE_SEQUENCE</param>
         </generator>
      </id>

      <timestamp name="updateTime" column="UPDATE_TIME" />

      <property name="siteCode" type="java.lang.Integer" column="SITE_CODE" length="6">
      </property>
      <set name="rooms" lazy="true" inverse="true" cascade="none">
         <key>
            <column name="SITE_ID" />
         </key>
         <one-to-many class="example.Room" />
      </set>
   </class>
</hibernate-mapping>


I ommited several fields and sets from it, but that's the spirit of things. I am thinking now that it has to do with the sets and relations I have between my objects.


Top
 Profile  
 
 Post subject: phew.. it's hard in your forum - but I found a solution
PostPosted: Thu Mar 09, 2006 11:36 am 
Newbie

Joined: Sat Feb 25, 2006 8:19 am
Posts: 7
just in case someone else gets a similar problem - I had my oracle10g table of update_time set on "Date", instead of "Timestamp". This caused the update query by the hibernate ("update ... where update_time = ...") to not recieve any rows (The db didn't find any such rows), and cause the exception.
After switching to Timestamp it worked just fine, and I am much happier now.

Thanks anyway :-)


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