File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate : cascade=all behaves like cascade=all,delete-orphan

 
Anirban Chowdhury
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hibernate version:3.6.2 final
Personal Oracle Database 11g Release 11.2.0.1.0 - Production

The relationship is like 1 organization can have multiple volunteers. However 1 volunteer may or may not be linked to an organization.
Organization.java

Volunteer.java


organization.hbm.xml

volunteer.hbm.xml




Ok, lets say there is 1 volunteer linked with this organization.
Then if we delink this volunteer from the organization and then call the delete on the organization, shouldn't it just leave an orphan volunteer?
It instead deletes the volunteer as well.



This generates the following SQL:



I think that's incorrect.Could you please suggest?
 
Manuel Petermann
Ranch Hand
Posts: 177
Hibernate Linux Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I know cascade all includes cascade orphan-delete so you might want do remove that.
I know what you want to do and the possible explanation i have might be wrong!
Explanation I have is:
Hibernate is a state machine and it just checks the state of the managed entities when you tell it to. Transaction.commit, session.flush, etc.
If you do all those things in one transaction hibernates just sees.
Before state check:
Relationship there!
After/in state check:
Relationship not there! Check if cascade orphan delete is set and remove all orphans.

This is my understanding of hibernate so far. Might be right or totally wrong so please feel free to correct me!
 
Anirban Chowdhury
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manuel, can you point me to some document according to which cascade = all included delete-orphan? AFAIK, it doesn't.
Thanks Anirban.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think an orphan is being deleted here.

An Organisation is being deleted and when this operation is called, Hibernate still thinks it is linked to the Volunteer instance. As cascade is set to ALL, this volunteer is removed when the organisation is deleted.

Try updating the organisation (to break the link with the volunteer) before deleting it.
 
Manuel Petermann
Ranch Hand
Posts: 177
Hibernate Linux Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reference manual explains that: "You can even use cascade="all" to specify that all operations should be cascaded along the association. The default cascade="none" specifies that no operations are to be cascaded."

Its a little vague though what all means in this context because the delete-orphan cascade is explained later on in the text. I would suggest it really means all cascades.
Found this: "Mapping an association (either a single valued association, or a collection) with cascade="all" marks the association as a parent/child style relationship where save/update/delete of the parent results in save/update/delete of the child or children."
Which is totally ok because in your case you don't want to cascade the delete at all as far as i understand.

Just try it and let us know.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic