I have two losely coupled objects. Currently I use lifecycle methods (onDelete) to perform an 'on cascade set null' operation. But there should be a more elegant way, shouldn't it?
I use an unordinary Category and Product scenario for demonstration but in real I need it for some other relations. By deleteing the category containing the product, the product should remain (not deleted) and its parent category property should be set to null.
But sadly, after deletion of the category the product would gets updated and since the its category property is not set to null, the category would be resaved. This raises the exception.
Hibernate version:
Hibernate 2.1.6
Mapping documents:
Snippets defining the relation:
Category:
[...]
<set name="products" lazy="true" inverse="true"
cascade="all" sort="unsorted">
<key column="CATEGORY_FK"/>
<one-to-many class="Product"/>
</set>
[...]
Product:
[...]
<many-to-one name="category" class="Category"
cascade="all" outer-join="auto" update="true"
insert="true" column="PRODUCT_FK" not-null="false"/>
[...]
Code between sessionFactory.openSession() and session.close():
Using Spring's Hibernate Dao Support it looks like:
public void remove(Category category) {
getHibernateTemplate().delete(category);
}
Full stack trace of any exception that occurs:
net.sf.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): 1, of class: xxx.Category
at net.sf.hibernate.impl.SessionImpl.forceFlush(SessionImpl.java:752)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:730)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1376)
at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)
at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:526)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:482)
at net.sf.hibernate.impl.SessionImpl.preFlushEntities(SessionImpl.java:2673)
at net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2250)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2239)
at org.ocsn.domain.dao.HibernateTestCase.flush(HibernateTestCase.java:79)
at xxx.dao.ProductDaoTest.testRemovingCategorySetsProductsCategoryPropertyToNull(ProductDaoTest.java:117)
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 org.jmock.extension.MockSupportingTestCase.runBare(MockSupportingTestCase.java:45)
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 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:
HSQL 1.7.2 (For testing, where the current problem was first noticed)
|