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?