Hi,
I am writing some JUnit test cases. I have a two one-to-many relationships across 3 tables and i am trying to delete a StaffData object from a table which in-turn i want the corresponding rows mapped to the Objective and StaffValue tables to be deleted. I have implemented these via stored procedures, however, i seem to keep getting the error stated below. I have no trouble at all inserting a single StaffData object which maps to inserting multiple Objective and StaffValue rows.
Any help at all would be much appreciated.
Regards
---------
Hibernate version: 3.2GA
Mapping documents:
StaffData.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping> <class name="staff.StaffData" table="Staff_Data_Hib"> <id name="staffId" type="string" column="StaffId" length="50"/> <property name="work" type="string" column="Work" length="50"/> <property name="busDay" type="timestamp" column="BusDay"/> <property name="inputTime" type="timestamp" column="InputTime"/> <property name="timeStamp" type="timestamp" column="Timestamp"/> <map name="Objectives" cascade="all, delete-orphan" inverse="true"> <key column="StaffId" foreign-key="staffId" on-delete="cascade"/> <map-key type="string" column="ShortName"/> <one-to-many class="staff.Objective"/> <loader query-ref="load_Objective"/> </map> <map name="StaffValues" cascade="all, delete-orphan" inverse="true"> <key column="KeyValue" foreign-key="staffId" on-delete="cascade"/> <map-key type="string" column="ValueType"/> <one-to-many class="staff.StaffValue"/> <loader query-ref="load_Staff_Value"/> </map> <loader query-ref="load_staff_data"/> <sql-insert callable="true" check="param"> { call insert_staff_data_hib ?, ?, ?, ?, ?, ? } </sql-insert> <sql-update callable="true" check="param"> { call update_staff_data_hib ?, ?, ?, ?, ?, ? } </sql-update> <sql-delete callable="true" check="param"> { call delete_staff_data_hib ?, ? } </sql-delete>-->
</class>
<sql-query name="load_staff_data" callable="true"> <return class="staff.StaffData"> <return-property name="staffId" column="StaffId"/> <return-property name="work" column="Work"/> <return-property name="busDay" column="BusDay"/> <return-property name="inputTime" column="InputTime"/> <return-property name="timeStamp" column="Timestamp"/> </return> { call load_staff_data_hib ? } </sql-query> </hibernate-mapping>
StaffValue.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping> <class name="staff.StaffValue" table="Staff_Values_Hib"> <composite-id name="valueCompositeKey" class="staff.ValueCompositeKey"> <key-property name="keyValue" type="string" column="KeyValue" length="50"/> <key-property name="valueType" type="string" column="ValueType" length="12"/> </composite-id> <property name="Value" type="string" column="Value" length="50"/>
<sql-insert callable="true" check="param"> { call insert_Staff_Value_hib ?, ?, ?, ? } </sql-insert> <sql-update callable="true" check="param"> { call update_Staff_Value_hib ?, ?, ?, ? } </sql-update> <sql-delete callable="true" check="param"> { call delete_Staff_Value_hib ?, ?, ? } </sql-delete>--> </class> <sql-query name="load_Staff_Value" callable="true"> <load-collection role="staff.StaffData.StaffValues" alias="loadStaffValues"/> <return class="staff.StaffValue"> <return-property name="ValueCompositeKey"> <return-column name="KeyValue"/> <return-column name="ValueType"/> </return-property> <return-property name="value" column="Value"/> </return> { call load_Staff_Value_hib ? } </sql-query> </hibernate-mapping>
Objective.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping> <class name="staff.Objective" table="Objective_Hib"> <composite-id name="ObjectiveCompositeKey" class="staff.ObjectiveCompositeKey"> <key-property name="shortName" type="string" column="ShortName" length="30"/> <key-property name="staffId" type="string" column="StaffId" length="50"/> </composite-id> <property name="timeTaken" type="float" column="TimeTaken"/> <sql-insert callable="true" check="param"> { call insert_Objective_hib ?, ?, ?, ? } </sql-insert> <sql-update callable="true" check="param"> { call update_Objective_hib ?, ?, ?, ? } </sql-update> <sql-delete callable="true" check="param"> { call delete_Objective_hib ?, ?, ? } </sql-delete>--> </class> <sql-query name="load_Objective" callable="true"> <load-collection role="staff.StaffData.Objectives" alias="loadObjectives"/> <return class="staff.Objective"> <return-property name="ObjectiveCompositeKey"> <return-column name="ShortName"/> <return-column name="StaffId"/> </return-property> <return-property name="timeTaken" column="TimeTaken"/> </return> { call load_Objective_hib ? } </sql-query> </hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
session.beginTransaction(); StaffData staff = (StaffData)session.load(StaffData.class, id); session.delete(id, staff); session.flush(); // <--- error occurs here!! session.getTransaction().commit(); session.close();
Full stack trace of any exception that occurs:
.12:45:39,465 INFO Environment:500 - Hibernate 3.2.0 12:45:39,512 INFO Environment:533 - hibernate.properties not found 12:45:39,512 INFO Environment:667 - Bytecode provider name : cglib 12:45:39,528 INFO Environment:584 - using JDK 1.4 java.sql.Timestamp handling 12:45:39,653 INFO Configuration:1350 - configuring from resource: /hibernate.cfg.xml 12:45:39,668 INFO Configuration:1327 - Configuration resource: /hibernate.cfg.xml 12:45:39,918 INFO Configuration:507 - Reading mappings from resource: staff/StaffData.hbm.xml 12:45:40,137 INFO HbmBinder:300 - Mapping class: staff.StaffData -> Staff_Data_Hib 12:45:40,246 INFO Configuration:507 - Reading mappings from resource: staff/StaffValue.hbm.xml 12:45:40,309 INFO HbmBinder:300 - Mapping class: staff.StaffValue -> Staff_Values_Hib 12:45:40,325 INFO Configuration:507 - Reading mappings from resource: staff/Objective.hbm.xml 12:45:40,387 INFO HbmBinder:300 - Mapping class: staff.Objective -> Objective_Hib 12:45:40,387 INFO Configuration:1465 - Configured SessionFactory: null 12:45:40,403 INFO HbmBinder:2375 - Mapping collection: staff.StaffData.Objectives -> Objective_Hib 12:45:40,403 INFO HbmBinder:2375 - Mapping collection: staff.StaffData.StaffValues -> Staff_Values_Hib 12:45:40,637 INFO C3P0ConnectionProvider:50 - C3P0 using driver: com.sybase.jdbc2.jdbc.SybDriver at URL: ****
12:45:40,637 INFO C3P0ConnectionProvider:51 - Connection properties: {user=****, password=****} 12:45:40,653 INFO C3P0ConnectionProvider:54 - autocommit mode: false 12:45:41,621 INFO SettingsFactory:81 - RDBMS: Adaptive Server Enterprise, version: Adaptive Server Enterprise/12.5.1/EBF 11429/P/Linux Intel/Enterprise Linux/ase1251/1823/32-bit/OPT/Tue Sep 16 23:43:54 2003
12:45:41,621 INFO SettingsFactory:82 - JDBC driver: jConnect (TM) for JDBC (TM), version: jConnect (TM) for JDBC(TM)/5.5(Build 25008)/P/JDK12/Tue May 29 14:37:46 2001
12:45:41,684 INFO Dialect:141 - Using dialect: org.hibernate.dialect.SybaseDialect 12:45:41,715 INFO TransactionFactoryFactory:31 - Using default transaction strategy (direct JDBC transactions) 12:45:41,715 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
12:45:41,731 INFO SettingsFactory:134 - Automatic flush during beforeCompletion(): disabled 12:45:41,731 INFO SettingsFactory:138 - Automatic session close at end of transaction: disabled 12:45:41,746 INFO SettingsFactory:153 - Scrollable result sets: enabled 12:45:41,746 INFO SettingsFactory:161 - JDBC3 getGeneratedKeys(): disabled 12:45:41,746 INFO SettingsFactory:169 - Connection release mode: auto 12:45:41,762 INFO SettingsFactory:196 - Default batch fetch size: 1 12:45:41,762 INFO SettingsFactory:200 - Generate SQL with comments: disabled 12:45:41,778 INFO SettingsFactory:204 - Order SQL updates by primary key: disabled 12:45:41,778 INFO SettingsFactory:369 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory 12:45:41,793 INFO ASTQueryTranslatorFactory:24 - Using ASTQueryTranslatorFactory 12:45:41,793 INFO SettingsFactory:212 - Query language substitutions: {} 12:45:41,809 INFO SettingsFactory:217 - JPA-QL strict compliance: disabled 12:45:41,809 INFO SettingsFactory:222 - Second-level cache: enabled 12:45:41,825 INFO SettingsFactory:226 - Query cache: disabled 12:45:41,825 INFO SettingsFactory:356 - Cache provider: org.hibernate.cache.NoCacheProvider 12:45:41,840 INFO SettingsFactory:241 - Optimize cache for minimal puts: disabled 12:45:41,840 INFO SettingsFactory:250 - Structured second-level cache entries: disabled 12:45:41,871 INFO SettingsFactory:270 - Echoing all SQL to stdout 12:45:41,871 INFO SettingsFactory:277 - Statistics: disabled 12:45:41,871 INFO SettingsFactory:281 - Deleted entity synthetic identifier rollback: disabled 12:45:41,887 INFO SettingsFactory:296 - Default entity-mode: pojo 12:45:41,950 INFO SessionFactoryImpl:161 - building session factory 12:45:42,543 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured 12:45:42,700 DEBUG SQL:393 - { call load_staff_data_hib ? } Hibernate: { call load_staff_data_hib ? } 12:45:42,715 DEBUG StringType:80 - binding 'TestID_1' to parameter: 1 12:45:42,746 DEBUG StringType:122 - returning 'TestID_1' as column: StaffId 12:45:42,762 DEBUG StringType:122 - returning 'TestWork' as column: Work 12:45:42,793 DEBUG TimestampType:122 - returning '2007-02-01 09:41:13' as column: BusDay 12:45:42,809 DEBUG TimestampType:122 - returning '2007-02-01 09:41:13' as column: InputTime 12:45:42,809 DEBUG TimestampType:122 - returning '2007-02-01 09:41:13' as column: Timestamp 12:45:42,856 DEBUG SQL:393 - { call load_Objective_hib ? } Hibernate: { call load_Objective_hib ? } 12:45:42,871 DEBUG StringType:80 - binding 'TestID_1' to parameter: 1 12:45:42,871 DEBUG StringType:122 - returning 'ShortName1' as column: ShortName 12:45:42,887 DEBUG StringType:122 - returning 'TestID_1' as column: StaffId 12:45:42,887 DEBUG StringType:122 - returning 'ShortName1' as column: ShortName 12:45:42,903 DEBUG StringType:122 - returning 'TestID_1' as column: StaffId 12:45:42,903 DEBUG FloatType:122 - returning '0.0' as column: TimeTaken 12:45:42,903 DEBUG StringType:122 - returning 'TestID_1' as column: StaffId 12:45:42,918 DEBUG StringType:122 - returning 'ShortName1' as column: ShortName 12:45:42,918 DEBUG StringType:122 - returning 'TestID_1' as column: StaffId 12:45:42,950 DEBUG StringType:122 - returning 'ShortName1' as column: ShortName 12:45:42,950 DEBUG StringType:122 - returning 'ShortName3' as column: ShortName 12:45:42,950 DEBUG StringType:122 - returning 'TestID_1' as column: StaffId 12:45:42,965 DEBUG StringType:122 - returning 'ShortName3' as column: ShortName 12:45:42,965 DEBUG StringType:122 - returning 'TestID_1' as column: StaffId 12:45:42,981 DEBUG FloatType:122 - returning '0.43231472' as column: TimeTaken 12:45:42,996 DEBUG StringType:122 - returning 'TestID_1' as column: StaffId 12:45:42,996 DEBUG StringType:122 - returning 'ShortName3' as column: ShortName 12:45:42,996 DEBUG StringType:122 - returning 'TestID_1' as column: StaffId 12:45:43,012 DEBUG StringType:122 - returning 'ShortName3' as column: ShortName 12:45:43,012 DEBUG StringType:122 - returning 'ShortName2' as column: ShortName 12:45:43,028 DEBUG StringType:122 - returning 'TestID_1' as column: StaffId 12:45:43,028 DEBUG StringType:122 - returning 'ShortName2' as column: ShortName 12:45:43,043 DEBUG StringType:122 - returning 'TestID_1' as column: StaffId 12:45:43,043 DEBUG FloatType:122 - returning '0.8787952' as column: TimeTaken 12:45:43,059 DEBUG StringType:122 - returning 'TestID_1' as column: StaffId 12:45:43,059 DEBUG StringType:122 - returning 'ShortName2' as column: ShortName 12:45:43,075 DEBUG StringType:122 - returning 'TestID_1' as column: StaffId 12:45:43,075 DEBUG StringType:122 - returning 'ShortName2' as column: ShortName 12:45:43,090 DEBUG SQL:393 - { call load_Staff_Value_hib ? } Hibernate: { call load_Staff_Value_hib ? } 12:45:43,106 DEBUG StringType:80 - binding 'TestID_1' to parameter: 1 12:45:43,106 DEBUG StringType:122 - returning 'TestID_1' as column: KeyValue 12:45:43,121 DEBUG StringType:122 - returning 'staff_TYPE1' as column: ValueType 12:45:43,121 DEBUG StringType:122 - returning 'TestID_1' as column: KeyValue 12:45:43,121 DEBUG StringType:122 - returning 'staff_TYPE1' as column: ValueType 12:45:43,137 DEBUG StringType:122 - returning 'Value1' as column: Value 12:45:43,137 DEBUG StringType:122 - returning 'TestID_1' as column: KeyValue 12:45:43,153 DEBUG StringType:122 - returning 'TestID_1' as column: KeyValue 12:45:43,153 DEBUG StringType:122 - returning 'staff_TYPE1' as column: ValueType 12:45:43,153 DEBUG StringType:122 - returning 'staff_TYPE1' as column: ValueType 12:45:43,168 DEBUG StringType:122 - returning 'TestID_1' as column: KeyValue 12:45:43,184 DEBUG StringType:122 - returning 'staff_TYPE2' as column: ValueType 12:45:43,184 DEBUG StringType:122 - returning 'TestID_1' as column: KeyValue 12:45:43,200 DEBUG StringType:122 - returning 'staff_TYPE2' as column: ValueType 12:45:43,200 DEBUG StringType:122 - returning 'Value2' as column: Value 12:45:43,215 DEBUG StringType:122 - returning 'TestID_1' as column: KeyValue 12:45:43,215 DEBUG StringType:122 - returning 'TestID_1' as column: KeyValue 12:45:43,215 DEBUG StringType:122 - returning 'staff_TYPE2' as column: ValueType 12:45:43,246 DEBUG StringType:122 - returning 'staff_TYPE2' as column: ValueType 12:45:43,262 DEBUG StringType:122 - returning 'TestID_1' as column: KeyValue 12:45:43,262 DEBUG StringType:122 - returning 'staff_TYPE3' as column: ValueType 12:45:43,262 DEBUG StringType:122 - returning 'TestID_1' as column: KeyValue 12:45:43,278 DEBUG StringType:122 - returning 'staff_TYPE3' as column: ValueType 12:45:43,278 DEBUG StringType:122 - returning 'Value1' as column: Value 12:45:43,309 DEBUG StringType:122 - returning 'TestID_1' as column: KeyValue 12:45:43,309 DEBUG StringType:122 - returning 'TestID_1' as column: KeyValue 12:45:43,309 DEBUG StringType:122 - returning 'staff_TYPE3' as column: ValueType 12:45:43,325 DEBUG StringType:122 - returning 'staff_TYPE3' as column: ValueType 12:45:43,340 DEBUG StringType:122 - returning 'TestID_1' as column: KeyValue 12:45:43,340 DEBUG StringType:122 - returning 'staff_TYPE4' as column: ValueType 12:45:43,340 DEBUG StringType:122 - returning 'TestID_1' as column: KeyValue 12:45:43,356 DEBUG StringType:122 - returning 'staff_TYPE4' as column: ValueType 12:45:43,356 DEBUG StringType:122 - returning 'Value2' as column: Value 12:45:43,371 DEBUG StringType:122 - returning 'TestID_1' as column: KeyValue 12:45:43,371 DEBUG StringType:122 - returning 'TestID_1' as column: KeyValue 12:45:43,371 DEBUG StringType:122 - returning 'staff_TYPE4' as column: ValueType 12:45:43,387 DEBUG StringType:122 - returning 'staff_TYPE4' as column: ValueType 12:45:43,403 DEBUG SQL:393 - { call delete_Objective_hib ?, ?, ? } Hibernate: { call delete_Objective_hib ?, ?, ? } 12:45:43,418 DEBUG StringType:80 - binding 'ShortName1' to parameter: 2 12:45:43,418 DEBUG StringType:80 - binding 'TestID_1' to parameter: 3 12:45:43,434 ERROR AbstractFlushingEventListener:301 - Could not synchronize database state with session org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [staff.Objective#StaffId: TestID_1; ShortName: ShortName1]
at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:1699) at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2458) at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2632) at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:73) 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:144) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) at test.staffTestCase.commitTransaction(staffTestCase.java:44) at test.staffTestCase.testDeletingstaffRemovesFromDatabase(staffTestCase.java:83) 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:585) 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.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.doRun(TestRunner.java:109) at junit.textui.TestRunner.run(TestRunner.java:72) at test.staffManagerTestSuite.main(staffManagerTestSuite.java:21) E Time: 4.109 There was 1 error: 1) testDeletingstaffRemovesFromDatabase(test.staffTestCase)org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [staff.Objective#StaffId: TestID_1; ShortName: ShortName1]
at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:1699) at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2458) at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2632) at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:73) 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:144) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) at test.staffTestCase.commitTransaction(staffTestCase.java:44) at test.staffTestCase.testDeletingstaffRemovesFromDatabase(staffTestCase.java:83) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at test.staffManagerTestSuite.main(staffManagerTestSuite.java:21)
FAILURES!!! Tests run: 1, Failures: 0, Errors: 1
Name and version of the database you are using: Sybase
The generated SQL (show_sql=true): included in the exception logs
Debug level Hibernate log excerpt: included in the exception logs
|