Solve the hibernate ed object wocould be re-Saved by cascade (remove deleted object from Associa

Source: Internet
Author: User

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. .

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.