gavin wrote:
If there is a difference, that is a bug. What difference do you think you have found?
Ok, i reproduced the behavior:
Code:
<class name="MediaSessionLevel" table="mediasessionlevel">
<id name="SQLId" column="msl_id" type="long" unsaved-value="null">
<generator class="native"/>
</id>
<bag name="bandwidths" table="bandwidth" cascade="all-delete-orphan">
<key column="msl_id" not-null="true"/>
<one-to-many class="MediaSessionLevel$Bandwidth"/>
</bag>
</class>
<class name="MediaSessionLevel$Bandwidth" table="bandwidth">
<id name="SQLId" column="ban_id" type="long" unsaved-value="null">
<generator class="native"/>
</id>
<property name="modifier" column="ban_modifier" type="string" length="31" not-null="true"/>
<property name="value" column="ban_value" type="long" not-null="true"/>
</class>
So i have a MediaSessionLevel with a getter which returns a Collection<Bandwidth>.
With the following code :
Code:
transaction = hSession.beginTransaction();
storedMsl = (MediaSessionLevel)hSession.load(MediaSessionLevel.class, id);
assertNotNull(storedMsl);
MediaSessionLevel updateMsl = new MediaSessionLevel();
assertEquals(updateMsl.getBandwidths().size(),0);
storedMsl.setAttributes(updateMsl.getAttributes());
storedMsl.setBandwidths(updateMsl.getBandwidths());
hSession.saveOrUpdate(storedMsl);
storedMsl = null;
transaction.commit();
I have the following exception :
Code:
org.hibernate.HibernateException: Don't dereference a collection with cascade="all-delete-orphan": visioconference.MediaSessionLevel.bandwidths
at org.hibernate.engine.Collections.updateUnreachableCollection(Collections.java:57)
at org.hibernate.event.AbstractFlushingEventListener.flushCollections(AbstractFlushingEventListener.java:208)
at org.hibernate.event.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:68)
at org.hibernate.event.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:23)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:719)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:84)
at visioconference.unittest.HibernateTest.testMediaSessionLevel(HibernateTest.java:95)
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.textui.TestRunner.doRun(TestRunner.java:116)
at junit.textui.TestRunner.start(TestRunner.java:172)
at junit.textui.TestRunner.main(TestRunner.java:138)
If i change cascade to "all,delete-orphan" i have no exception.
I have to sniff a network, so i construct a new object, get this equivalent stored in db, set the new collections (and i will have collection of objects which have too some collection of objects) and save the new data. I don't know if it's the best practices.