I'm sure what you meant by "manually setting the id on A". Since I'm trying to insert a new row, I think Hibernate is setting the Id for me.
Anyway, I did another test. This time I create an empty set (no children) and associate it to the new parent object. Hibernate still tries to UPDATE the newly created parent object and raises the StaleObjectStateException. I can kind of understand why Hibernate may think it needs to update the timestamp of the parent row since the parent has assoc. with a set (even though it's an empty!?). But I'm confused why it raises the exception on the new row inserted within the same saveOrUpdate() call.... Is this an intended feature?
Here's the Hibernate log:
[2/02/04 16:18:49:641 EST] main l.SessionImpl G net.sf.hibernate.impl.SessionImpl opened session
[2/02/04 16:18:49:651 EST] main BCTransaction G net.sf.hibernate.transaction.JDBCTransaction begin
[2/02/04 16:18:49:661 EST] main ctionProvider G net.sf.hibernate.connection.DriverManagerConnectionProvider total checked-out connections: 0
[2/02/04 16:18:49:661 EST] main ctionProvider G net.sf.hibernate.connection.DriverManagerConnectionProvider using pooled JDBC connection, pool size: 0
[2/02/04 16:18:49:661 EST] main BCTransaction G net.sf.hibernate.transaction.JDBCTransaction current autocommit status:false
[2/02/04 16:18:54:698 EST] main l.SessionImpl G net.sf.hibernate.impl.SessionImpl saveOrUpdate() unsaved instance
[2/02/04 16:18:54:718 EST] main l.BatcherImpl G net.sf.hibernate.impl.BatcherImpl about to open: 0 open PreparedStatements, 0 open ResultSets
[2/02/04 16:18:54:738 EST] main l.BatcherImpl G net.sf.hibernate.impl.BatcherImpl prepared statement get: select SHEET_HEADER_SEQ_ID.nextval from dual
[2/02/04 16:18:54:748 EST] main l.BatcherImpl G net.sf.hibernate.impl.BatcherImpl preparing statement
[2/02/04 16:18:54:779 EST] main enceGenerator G net.sf.hibernate.id.SequenceGenerator Sequence identifier generated: 31
[2/02/04 16:18:54:789 EST] main l.BatcherImpl G net.sf.hibernate.impl.BatcherImpl done closing: 0 open PreparedStatements, 0 open ResultSets
[2/02/04 16:18:54:809 EST] main l.BatcherImpl G net.sf.hibernate.impl.BatcherImpl closing statement
[2/02/04 16:18:54:829 EST] main l.SessionImpl G net.sf.hibernate.impl.SessionImpl saving [com.epacube.data.sheet.Sheet#31]
[2/02/04 16:18:54:839 EST] main gine.Cascades G net.sf.hibernate.engine.Cascades processing cascades for: com.epacube.data.sheet.Sheet
[2/02/04 16:18:54:859 EST] main gine.Cascades G net.sf.hibernate.engine.Cascades done processing cascades for: com.epacube.data.sheet.Sheet
[2/02/04 16:18:54:869 EST] main ne.Versioning G net.sf.hibernate.engine.Versioning Seeding: 2004-02-02 16:18:54.869
[2/02/04 16:18:54:889 EST] main gine.Cascades G net.sf.hibernate.engine.Cascades$17 version unsaved-value strategy UNDEFINED
[2/02/04 16:18:54:909 EST] main gine.Cascades G net.sf.hibernate.engine.Cascades$15 id unsaved-value strategy NULL
[2/02/04 16:18:54:929 EST] main gine.Cascades G net.sf.hibernate.engine.Cascades$15 id unsaved-value strategy NULL
[2/02/04 16:18:54:939 EST] main gine.Cascades G net.sf.hibernate.engine.Cascades$15 id unsaved-value strategy NULL
[2/02/04 16:18:54:979 EST] main gine.Cascades G net.sf.hibernate.engine.Cascades processing cascades for: com.epacube.data.sheet.Sheet
[2/02/04 16:18:54:999 EST] main gine.Cascades G net.sf.hibernate.engine.Cascades done processing cascades for: com.epacube.data.sheet.Sheet
[2/02/04 16:19:37:643 EST] main BCTransaction G net.sf.hibernate.transaction.JDBCTransaction commit
[2/02/04 16:19:37:663 EST] main l.SessionImpl G net.sf.hibernate.impl.SessionImpl flushing session
[2/02/04 16:19:37:683 EST] main gine.Cascades G net.sf.hibernate.engine.Cascades processing cascades for: com.epacube.data.sheet.Sheet
[2/02/04 16:19:37:703 EST] main gine.Cascades G net.sf.hibernate.engine.Cascades done processing cascades for: com.epacube.data.sheet.Sheet
[2/02/04 16:19:37:723 EST] main l.SessionImpl G net.sf.hibernate.impl.SessionImpl Flushing entities and processing referenced collections
[2/02/04 16:19:37:773 EST] main l.WrapVisitor G net.sf.hibernate.impl.WrapVisitor Wrapped collection in role: com.epacube.data.sheet.Sheet.comments
[2/02/04 16:19:37:803 EST] main tityPersister G net.sf.hibernate.persister.AbstractEntityPersister com.epacube.data.sheet.Sheet.comments is dirty
[2/02/04 16:19:37:823 EST] main l.SessionImpl G net.sf.hibernate.impl.SessionImpl Updating entity: [com.epacube.data.sheet.Sheet#31]
[2/02/04 16:19:37:843 EST] main ne.Versioning G net.sf.hibernate.engine.Versioning Incrementing: 2004-02-02 16:18:54.869 to 2004-02-02 16:19:37.843
[2/02/04 16:19:37:874 EST] main l.SessionImpl G net.sf.hibernate.impl.SessionImpl Collection found: [com.epacube.data.sheet.Sheet.comments#31], was: [<unreferenced>]
[2/02/04 16:19:37:894 EST] main l.SessionImpl G net.sf.hibernate.impl.SessionImpl Processing unreferenced collections
[2/02/04 16:19:37:904 EST] main l.SessionImpl G net.sf.hibernate.impl.SessionImpl Scheduling collection removes/(re)creates/updates
[2/02/04 16:19:37:934 EST] main l.SessionImpl G net.sf.hibernate.impl.SessionImpl Flushed: 1 insertions, 1 updates, 0 deletions to 1 objects
[2/02/04 16:19:37:944 EST] main l.SessionImpl G net.sf.hibernate.impl.SessionImpl Flushed: 1 (re)creations, 0 updates, 0 removals to 1 collections
[2/02/04 16:19:37:944 EST] main .impl.Printer G net.sf.hibernate.impl.Printer listing entities:
[2/02/04 16:19:37:964 EST] main .impl.Printer G net.sf.hibernate.impl.Printer com.epacube.data.sheet.Sheet{comments=[], subsheets=null, code=CHON TEST1, startDate=02 February 2004 16:18:47, entityStructures=null, updateTimestamp=02 February 2004 16:19:37, status=CodeRef#1, endDate=null, ownerOrgEntity=EntityStructure#739, name=CHON TEST, basisTypes=null, id=31, updateUser=null, sheetClass=SheetClass#884265}
[2/02/04 16:19:37:974 EST] main l.SessionImpl G net.sf.hibernate.impl.SessionImpl executing flush
[2/02/04 16:19:37:984 EST] main tityPersister G net.sf.hibernate.persister.EntityPersister Inserting entity: [com.epacube.data.sheet.Sheet#31]
[2/02/04 16:19:37:994 EST] main tityPersister G net.sf.hibernate.persister.EntityPersister Version: 2004-02-02 16:18:54.869
[2/02/04 16:19:37:994 EST] main l.BatcherImpl G net.sf.hibernate.impl.BatcherImpl about to open: 0 open PreparedStatements, 0 open ResultSets
[2/02/04 16:19:38:004 EST] main l.BatcherImpl G net.sf.hibernate.impl.BatcherImpl prepared statement get: insert into SHEET_HEADER (UPDATE_TIMESTAMP, HEADER_CODE, START_DATE, END_DATE, HEADER_NAME, SHEET_CLASS_FK, OWNER_ORG_ENTITY_FK, SHEET_STATUS_CR_FK, UPDATE_USER_FK, SHEET_HEADER_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
[2/02/04 16:19:38:014 EST] main l.BatcherImpl G net.sf.hibernate.impl.BatcherImpl preparing statement
[2/02/04 16:19:38:024 EST] main tityPersister G net.sf.hibernate.persister.EntityPersister Dehydrating entity: [com.epacube.data.sheet.Sheet#31]
[2/02/04 16:19:38:024 EST] main TimestampType G net.sf.hibernate.type.NullableType binding '02 February 2004 16:18:54' to parameter: 1
[2/02/04 16:19:38:034 EST] main pe.StringType G net.sf.hibernate.type.NullableType binding 'CHON TEST1' to parameter: 2
[2/02/04 16:19:38:054 EST] main TimestampType G net.sf.hibernate.type.NullableType binding '02 February 2004 16:18:47' to parameter: 3
[2/02/04 16:19:38:054 EST] main TimestampType G net.sf.hibernate.type.NullableType binding null to parameter: 4
[2/02/04 16:19:38:064 EST] main pe.StringType G net.sf.hibernate.type.NullableType binding 'CHON TEST' to parameter: 5
[2/02/04 16:19:38:074 EST] main gine.Cascades G net.sf.hibernate.engine.Cascades$17 version unsaved-value strategy UNDEFINED
[2/02/04 16:19:38:074 EST] main gine.Cascades G net.sf.hibernate.engine.Cascades$15 id unsaved-value strategy NULL
[2/02/04 16:19:38:084 EST] main type.LongType G net.sf.hibernate.type.NullableType binding '884265' to parameter: 6
[2/02/04 16:19:38:084 EST] main gine.Cascades G net.sf.hibernate.engine.Cascades$15 id unsaved-value strategy NULL
[2/02/04 16:19:38:094 EST] main type.LongType G net.sf.hibernate.type.NullableType binding '739' to parameter: 7
[2/02/04 16:19:38:104 EST] main gine.Cascades G net.sf.hibernate.engine.Cascades$15 id unsaved-value strategy NULL
[2/02/04 16:19:38:114 EST] main type.LongType G net.sf.hibernate.type.NullableType binding '1' to parameter: 8
[2/02/04 16:19:38:114 EST] main type.LongType G net.sf.hibernate.type.NullableType binding null to parameter: 9
[2/02/04 16:19:38:124 EST] main type.LongType G net.sf.hibernate.type.NullableType binding '31' to parameter: 10
[2/02/04 16:19:38:134 EST] main l.BatcherImpl G net.sf.hibernate.impl.BatchingBatcher Adding to batch
[2/02/04 16:19:38:144 EST] main l.BatcherImpl G net.sf.hibernate.impl.BatchingBatcher Executing batch size: 1
[2/02/04 16:19:38:154 EST] main l.BatcherImpl G net.sf.hibernate.impl.BatchingBatcher success of batch update unknown: 0
[2/02/04 16:19:38:154 EST] main l.BatcherImpl G net.sf.hibernate.impl.BatcherImpl done closing: 0 open PreparedStatements, 0 open ResultSets
[2/02/04 16:19:38:164 EST] main l.BatcherImpl G net.sf.hibernate.impl.BatcherImpl closing statement
[2/02/04 16:19:38:164 EST] main tityPersister G net.sf.hibernate.persister.EntityPersister Updating entity: [com.epacube.data.sheet.Sheet#31]
[2/02/04 16:19:38:174 EST] main tityPersister G net.sf.hibernate.persister.EntityPersister Existing version: 2004-02-02 16:18:54.869 -> New version: 2004-02-02 16:19:37.843
[2/02/04 16:19:38:184 EST] main l.BatcherImpl G net.sf.hibernate.impl.BatcherImpl about to open: 0 open PreparedStatements, 0 open ResultSets
[2/02/04 16:19:38:184 EST] main l.BatcherImpl G net.sf.hibernate.impl.BatcherImpl prepared statement get: update SHEET_HEADER set UPDATE_TIMESTAMP=?, HEADER_CODE=?, START_DATE=?, END_DATE=?, HEADER_NAME=?, SHEET_CLASS_FK=?, OWNER_ORG_ENTITY_FK=?, SHEET_STATUS_CR_FK=?, UPDATE_USER_FK=? where SHEET_HEADER_ID=? and UPDATE_TIMESTAMP=?
[2/02/04 16:19:38:194 EST] main l.BatcherImpl G net.sf.hibernate.impl.BatcherImpl preparing statement
[2/02/04 16:19:38:204 EST] main tityPersister G net.sf.hibernate.persister.EntityPersister Dehydrating entity: [com.epacube.data.sheet.Sheet#31]
[2/02/04 16:19:38:204 EST] main TimestampType G net.sf.hibernate.type.NullableType binding '02 February 2004 16:19:37' to parameter: 1
[2/02/04 16:19:38:214 EST] main pe.StringType G net.sf.hibernate.type.NullableType binding 'CHON TEST1' to parameter: 2
[2/02/04 16:19:38:224 EST] main TimestampType G net.sf.hibernate.type.NullableType binding '02 February 2004 16:18:47' to parameter: 3
[2/02/04 16:19:38:234 EST] main TimestampType G net.sf.hibernate.type.NullableType binding null to parameter: 4
[2/02/04 16:19:38:234 EST] main pe.StringType G net.sf.hibernate.type.NullableType binding 'CHON TEST' to parameter: 5
[2/02/04 16:19:38:244 EST] main gine.Cascades G net.sf.hibernate.engine.Cascades$17 version unsaved-value strategy UNDEFINED
[2/02/04 16:19:38:244 EST] main gine.Cascades G net.sf.hibernate.engine.Cascades$15 id unsaved-value strategy NULL
[2/02/04 16:19:38:254 EST] main type.LongType G net.sf.hibernate.type.NullableType binding '884265' to parameter: 6
[2/02/04 16:19:38:264 EST] main gine.Cascades G net.sf.hibernate.engine.Cascades$15 id unsaved-value strategy NULL
[2/02/04 16:19:38:274 EST] main type.LongType G net.sf.hibernate.type.NullableType binding '739' to parameter: 7
[2/02/04 16:19:38:284 EST] main gine.Cascades G net.sf.hibernate.engine.Cascades$15 id unsaved-value strategy NULL
[2/02/04 16:19:38:294 EST] main type.LongType G net.sf.hibernate.type.NullableType binding '1' to parameter: 8
[2/02/04 16:19:38:294 EST] main type.LongType G net.sf.hibernate.type.NullableType binding null to parameter: 9
[2/02/04 16:19:38:304 EST] main type.LongType G net.sf.hibernate.type.NullableType binding '31' to parameter: 10
[2/02/04 16:19:38:314 EST] main TimestampType G net.sf.hibernate.type.NullableType binding '02 February 2004 16:18:54' to parameter: 11
[2/02/04 16:19:38:334 EST] main tateException R net.sf.hibernate.StaleObjectStateException An operation failed due to stale data
net.sf.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) for com.epacube.data.sheet.Sheet instance with identifier: 31
at net.sf.hibernate.persister.AbstractEntityPersister.check(AbstractEntityPersister.java:498)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:697)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:668)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:53)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2306)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2260)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2185)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at com.epacube.dao.HibernateDAO.save(HibernateDAO.java:153)
at com.epacube.sheet.SheetWorker.addSheet(SheetWorker.java:62)
at com.epacube.sheet.SheetManager.addSheet(SheetManager.java:29)
at com.epacube.sheet.AddSheetAction.execute(AddSheetAction.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at com.opensymphony.xwork.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:304)
at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:174)
at com.opensymphony.xwork.interceptor.AroundInterceptor.intercept(AroundInterceptor.java:37)
at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:169)
at com.opensymphony.xwork.interceptor.AroundInterceptor.intercept(AroundInterceptor.java:37)
at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:169)
at com.opensymphony.xwork.interceptor.AroundInterceptor.intercept(AroundInterceptor.java:37)
at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:169)
at com.opensymphony.xwork.interceptor.AroundInterceptor.intercept(AroundInterceptor.java:37)
at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:169)
at com.opensymphony.xwork.DefaultActionProxy.execute(DefaultActionProxy.java:116)
at com.epacube.web.xwork.ActionInvocationWrapper.execute(ActionInvocationWrapper.java:76)
at com.epacube.sheet.AddSheetActionTest.testAddSheet(AddSheetActionTest.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at junit.textui.TestRunner.doRun(TestRunner.java:116)
at junit.textui.TestRunner.start(TestRunner.java:172)
at com.intellij.rt.execution.junit.TextTestRunner.main(TextTestRunner.java:12)
|