I've got an interesting bug in the way I'm using Hibernate. I'm getting a when I try and cascade delete multiple object within a transaction. So, with my code looking like this:
Code:
PersistenceManager.beginTransaction();
PersistenceManager.saveOrUpdate(product);
PersistenceManager.saveOrUpdate(customer);
PersistenceManager.saveOrUpdate(proposal);
PersistenceManager.commitTransaction();
/** do some validations here */
PersistenceManager.beginTransaction();
PersistenceManager.delete(proposal);
PersistenceManager.delete(customer);
PersistenceManager.delete(product);
PersistenceManager.commitTransaction();
I get a a net.sf.hibernate.NonUniqueObjectException exception when stepping over the
PersistenceManager.delete(customer); snippet. (See stack trace below). Now if I modify my code to commit after every delete like this:
Code:
PersistenceManager.saveOrUpdate(product);
PersistenceManager.saveOrUpdate(customer);
PersistenceManager.saveOrUpdate(proposal);
PersistenceManager.commitTransaction();
/** do some validations here */
PersistenceManager.beginTransaction();
PersistenceManager.delete(proposal);
PersistenceManager.commitTransaction();
PersistenceManager.beginTransaction();
PersistenceManager.delete(customer);
PersistenceManager.commitTransaction();
PersistenceManager.beginTransaction();
PersistenceManager.delete(product);
PersistenceManager.commitTransaction();
Things work just fine and all the deletes cascade properly. Can anyone help me?
Hibernate version: 2.1.6 Full stack trace of any exception that occurs:net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: ff808082fe76b83200fe76b836400002, of class: com.ap.ptool.biz.Customer
at net.sf.hibernate.impl.SessionImpl.delete(SessionImpl.java:1141)
at com.ap.ptool.persistence.PersistenceManager.delete(PersistenceManager.java:92)
at com.ap.ptool.biz.tests.unit.SolutionTest.testCreateSolutionWithProposedProductOne_Ok(SolutionTest.java:48)
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 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:421)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:305)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:186)
Name and version of the database you are using:MySQL v4
Debug level Hibernate log excerpt:Code: