File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Hibernate :  cascade=all behaves like cascade=all,delete-orphan Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate :  cascade=all behaves like cascade=all,delete-orphan" Watch "Hibernate :  cascade=all behaves like cascade=all,delete-orphan" New topic

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

Anirban Chowdhury
Ranch Hand

Joined: Aug 05, 2008
Posts: 36
Hibernate version:3.6.2 final
Personal Oracle Database 11g Release - Production

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



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?

To living life on the edge! I blog my experiences @
Manuel Petermann
Ranch Hand

Joined: Jul 19, 2011
Posts: 177

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!

Please correct my English.
Anirban Chowdhury
Ranch Hand

Joined: Aug 05, 2008
Posts: 36
Manuel, can you point me to some document according to which cascade = all included delete-orphan? AFAIK, it doesn't.
Thanks Anirban.
James Boswell

Joined: Nov 09, 2011
Posts: 1051

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

Joined: Jul 19, 2011
Posts: 177

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.
I agree. Here's the link:
subject: Hibernate : cascade=all behaves like cascade=all,delete-orphan
jQuery in Action, 3rd edition