Hello,
it seems to me that the cascade options have changed from hibernate 2 to hibernate 3 when deleting an object.
I have an n:m relation that is viewed as a 1:n and an n:1 relation.
So I have 3 Tables, lets call them A, AB, B.
In Object Terms Object A has a Set of Object AB and Object B has a Set of Object AB. AB itself holds a reference to A and a reference to B.
When deleting Object A I also want to delete the Entries in the HashSet automatically (means I want to delete all entries in the HashSet that holds references to Object AB).
My first solution was to delete Object A and hope that Hibernate deletes the references to Object AB in the Table AB. If I do so it depends on the cascade settings what Hibernate will do. If I set cascade="all-delete-orphan" all Objects that have references to AB are deleted. If I use cascade="save-update" I get the error 'deleted object would be re-saved by cascade ...'
Both behaviours are not the expected one.
I get the expected result, if I remove the Objects AB from Object A (means I initialize the Set with a new HashSet and store Object A). The problem with this approach is, that the entries in Table AB are not deleted. Only the reference to Object A is set to NULL.
So now folks, what is the right configuration to let Hibernate cascade from Object A to Object AB and delete Object A as well as all referenced Objects AB in the set of A?
Some good examples would be really good help!
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 3.0.4
Mapping documents:
<hibernate-mapping>
<class name="at.sozvers.seucc.book.bo.AuthorBO" table="AUTHOR" lazy="false">
<id name="id" column="id" type="java.lang.Long" unsaved-value="null">
<generator class="org.hibernate.id.SequenceGenerator">
<param name="sequence">hibernate_sequence</param>
</generator>
</id>
<version name="version" type="java.lang.Long" column="VERSION" access="property" unsaved-value="undefined"/>
<property name="akadGrad" type="java.lang.String" column="akadGrad" length="30" not-null="false"/>
<property name="geburtsdatum" type="java.lang.String" column="geburtsdatum" length="15" not-null="false"/>
<property name="geschlecht" type="java.lang.String" column="geschlecht" length="1" not-null="false"/>
<set name="bookSet" lazy="false" cascade="all-delete-orphan" sort="unsorted">
<key column="author"></key>
<one-to-many class="at.sozvers.seucc.book.bo.BookAuthorBO"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="at.sozvers.seucc.book.bo.BookAuthorBO" table="BOOKAUTHOR">
<id name="id" column="id" type="java.lang.Long" unsaved-value="null">
<generator class="org.hibernate.id.SequenceGenerator">
<param name="sequence">hibernate_sequence</param>
</generator>
</id>
<version name="version" type="java.lang.Long" column="VERSION" access="property" unsaved-value="undefined"/>
<many-to-one name="author" class="at.sozvers.seucc.book.bo.AuthorBO" cascade="delete" outer-join="auto" column="author"/>
<many-to-one name="book" class="at.sozvers.seucc.book.bo.BookBO" cascade="delete" outer-join="auto"column="book"/>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="at.sozvers.seucc.book.bo.BookBO" table="BOOK" lazy="false">
<id name="id" column="id" type="java.lang.Long" unsaved-value="null">
<generator class="org.hibernate.id.SequenceGenerator">
<param name="sequence">hibernate_sequence</param>
</generator>
</id>
<version name="version" type="java.lang.Long" column="VERSION" access="property" unsaved-value="undefined" />
<property name="isbn" type="java.lang.String" column="isbn" length="15" not-null="true" />
<property name="title" type="java.lang.String" column="title" length="100" not-null="true" />
<set name="authorSet" lazy="false" cascade="all-delete-orphan" sort="unsorted" >
<key column="book" > </key>
<one-to-many class="at.sozvers.seucc.book.bo.BookAuthorBO"/>
</set>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
using Spring:
getHibernateTemplate().delete(object);
Full stack trace of any exception that occurs:
Name and version of the database you are using: Postgresql 8.0.1
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt: