HIbernate Cascade Delete

Source: Internet
Author: User

In a one-to-many scenario such as Cinema-> screen;

1, normal in the case of no cascade (CasCade) Delete one of the parties (cinema) will report a Foreign Key association exception screen contains Cinema's foreign key;

This time you can remove all screens in cinema after removing all screens, and then delete cinema so that it is OK;

2, but in the case of cascading, in the one side of the cinema set Cascadetype.all (so that when saving a cinema, it is convenient to save all screen), in

Delete more than one of the above methods screen will be an error deleted object would is re-saved by Cascade

Workaround:

Method 1 Removes the cascade of the set party: that is, to cancel the Cascadetype.all set in cinema so it loses its meaning.
Method 2 resolves the association, then deletes it (removes it from the party after it has been removed and sets the foreign key of the multiparty to null, which is removed from the multiparty)

Onside.getmanys (). Remove (Thismany); Removes the object that is currently being deleted in the set of the associated party
Thismany.setone (NULL); Sets the corresponding party to be empty, releasing the relationship between them
Manydao.delete (Thismany);
Method 3 adds cascade to the Many-to-one side but the value cannot be none, which may affect a party when doing some operations on a multi-party screen (undesirable)

Where Method 2 is preferable:

HIbernate Cascade Delete

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.