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