-->
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.  [ 3 posts ] 
Author Message
 Post subject: updating a row in the database
PostPosted: Wed Mar 08, 2006 8:54 am 
Newbie

Joined: Wed Mar 08, 2006 8:11 am
Posts: 17
Location: Israel
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0.5

Full stack trace of any exception that occurs:
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)

Name and version of the database you are using: Oracle10g

The generated SQL (show_sql=true):
Hibernate: update SITE set UPDATE_TIME=?, SITE_CODE=?, SITE_TYPE=?, REGION=?, WORK_MODE=?, DEFAULT_CODEC=?, VERSION=?, CREATION_TIME=? where SITE_ID=? and UPDATE_TIME=?

I am trying to fill my database with random data (which corresponds to my constraints). At first I created the different lists of objects, and saved each list in it's own saveCollection method, which opened a session, started a transaction, iterated over the list and at the end commited the transaction and closed the session.
This worked allright at the beginning, but I then had to put some objects in a HashSet - I did overwrite the HashCode method of my objects to return the pk.HashCode(), and of course there is no Pk before I save the object the first time - so I needed to first save the list, get the pk, and then add to the object some more data, and put it in the HashSet (For example - I had to add a Station object to the current StationSetup object, and then add the StationSetup to a HashSet in the Station object)
So I added another method, to update the collection - iterating over it, and running update (or later, saveOrUpdate) over the objects in the list. That is where I got the above exception.
As far as I can see - of course the row was updated by another transaction. I have just finished adding it to the database, and closed the transactions not more than 10 lines ago...
I then tried something else - to use one long session over my entire database filling method. This way the saveCollection and updateCollection methods only start and commit a transaction. Now I get that same exception much earlier. First fill methods creats a list of Sites and saves them, second creates a list of Rooms and tries to save them. Each room has one Site, each Site has a HashSet of Rooms. This is where Hibernate tries to update the current Site object in the database, and crashes - Why can't I create a new site, save it to the data base, then add some info to it, and update it through saving the Room it is associated with?
Phew, what am I missing here? should I open and close the session for each saveCollection method, or is keeping one long session opened for the entire database fill method a prefered approach? And what should I do either way, to get it to work properly?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 9:13 am 
Newbie

Joined: Mon Mar 06, 2006 5:20 am
Posts: 7
Could you put here the relevant portion of code ?

_________________
Don't forget to rate useful posts


Top
 Profile  
 
 Post subject: here's the code
PostPosted: Wed Mar 08, 2006 9:51 am 
Newbie

Joined: Wed Mar 08, 2006 8:11 am
Posts: 17
Location: Israel
http://rafb.net/paste/results/PZ9scK87.html

This code finally works - but here's some info about it -

PresetConfig is a subclass of StationConfig. At first I create several PresetConfig objects, and add them to the list statConfigList, after initializing some of their properties. (lines 2-5)

Then I Call the saveCollection method, to iterate over the list, and save the objects. I need them saved, because later I am putting them into a HashSet, and I need their generated PK to be present by then. (line 7)

Next I open a new session, and inside the session I iterate over the list again, this time updating some of the properties in the StationConfig object inside setSetupCommon method (line 11). This method puts the object inside a HashSet, as mentioned earlier. After thie iteration, I flush the session and close it.

I guess Hibernate sees their id field as not null (It's of type Integer), and undestands they are detached objects, so the flush needs to call update on them.

I am still wondering how come I can't just take a presistent object inside a session, close the session and make it detached, update it, change it, crunch it around, and then later just use some other method that opens a new session, re-attaches the object, and updates the database. How do I do it?


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