Halo!
Here is my mapping:
Code:
<hibernate-mapping>
<class name="foo.model.Cat" table="cats">
<id name="id" access="field">
<generator class="native" />
</id>
<version name="version" access="field" />
<property name="name" length="512" />
<list name="kittens" batch-size="150" cascade="all" inverse="false" access="field">
<key column="parent_id" not-null="true" />
<list-index column="ordr" />
<one-to-many class="foo.model.Kitten" />
</list>
</class>
<class name="foo.model.Kitten" table="kittens">
<id name="id" access="field">
<generator class="native" />
</id>
<version name="version" access="field" />
<property name="name" length="512" />
<many-to-one name="parent" column="parent_id" class="foo.model.Cat" insert="false" update="false" not-null="true" />
</class>
</hibernate-mapping>
The cascade for the indexed collection is "all" and not "all-delete-orphan" because of this issue:
http://forum.hibernate.org/viewtopic.php?p=2350533as I was not able to change the parent of a kitten without getting an exception, so I ended up with an "all". When I try to remove a kitten from the underlying collection, hibernate seems to be performing updates to reorder the list of kittens. So for a Cat#1 with Kitten#1, Kitten#2 and Kitten#3, I get this when I try to remove Kitten#2:
Code:
DEBUG (AbstractCollectionPersister.java:1515) - Updating rows of collection: foo.model.Cat.kittens#2
DEBUG (AbstractBatcher.java:366) - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG (AbstractBatcher.java:401) - update kittens set parent_id=?, ordr=? where id=?
TRACE (AbstractBatcher.java:484) - preparing statement
TRACE (NullableType.java:133) - binding '1' to parameter: 1
TRACE (NullableType.java:133) - binding '1' to parameter: 2
TRACE (NullableType.java:133) - binding '3' to parameter: 3
DEBUG (AbstractCollectionPersister.java:1522) - done updating rows: 1 updated
Seems to work fine for this case, but what happens when I try to delete Kitten#3 or the whole collection? The answer is nothing. For these cases, there is no ordering to be updated, so simply nothing happens.
The only workaround I managed to come with is removing the not-null constraints and let hibernate set the parents of the kittens I want to delete to null. I am not happy with that solution though, because it messes up with my database integrity.
Is there a possibility to fix this in an upcoming release? Or fix the "deleted object would be re-saved by cascade" issue that forces me remove the delete-orphan cascade?
Any other solution on this issue?
Thanks,
akz