-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: deleted object would be re-saved by cascade ...
PostPosted: Mon Jun 20, 2005 6:11 pm 
Beginner
Beginner

Joined: Sun Mar 28, 2004 9:01 am
Posts: 21
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:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 4:00 pm 
Beginner
Beginner

Joined: Sun Mar 28, 2004 9:01 am
Posts: 21
Found one info that told me to clean the set instead of setting it to an empty set. This helps in the case that the whole entry in the AB Table is removed, but still the cascade options are ignored by Hibernate.

Why do I have to clean the set myself. Shouldn't Hibernate do the work for me. Hibernate 2 did quite a better job in this area? I just want Hibernate to cascade to the Table AB and delete the entries that are associated to Table A.

Where are the Hibernate Users around? Did no one had similar problems with Version 3.0.x?

Thx Rene


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 10:16 am 
Newbie

Joined: Thu Aug 25, 2005 10:04 am
Posts: 6
I am having the exact same problems. Do you have any further information? I think (with my limited hibernate experience) the cascade="delete" on the many-to-one element of the AB object is wrong, you don't want the A and B objects deleted, when the AB object is deleted.

I can't really think about a good solution: When I delete a A object I don't want to tamper with the List of AB objects in B, that defeats the purpose of cascade deletes. Is there any good solution?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.