When you are working on a project today, You need to delete an object. Because the association relationship is one-to-many and one-to-oneCodeWhen multiple objects need to be deleted
Deleted Object wocould be re-Saved by cascade (remove deleted object from associations)
Exception: After a variety of Google and Baidu, I found that there are several ways to solve this problem on the Internet:
Method 1 Delete cascade of the set party :(Disadvantage: Sub-objects and parent objects cannot be updated in cascade mode, which is meaningless.)
Method 2 adds cascade to the allow-to-one side, but the value cannot be none (Disadvantage: Sub-objects are also cascaded to the parent object. Deleting a sub-object will also delete the parent object, and it cannot be obtained.)
Method 3(The test is successful andConvenient),First look at the following code:
Order = (Order) session. get (Order. class, 2); Order. getcustormer (). getorders (). remove (order); Order. setcustormer (null); Session. delete (order );
Order is the one with multiple values. If order is to be deletedAn order object that directly calls session. Delete() The method will certainly reportOpenHeader object
to solve this problem, first, you can obtain the one that corresponds to the order object (that is, the customer object) , delete the order object from the customer object, and set the customer object of this Order object to null, so that the exception will not occur when the deletion is executed. .