Hibernate:deleted object would is re-saved by cascade (remove deleted object from associations): []

Source: Internet
Author: User

Exceptions for Hibernate multiple table associations: Deleted object would be re-saved by cascade (remove deleted object from associations): [].
A one-to-many (many-to-one) association between the "cause of Reason" table, two cases: double-table pair Toto one, single-table self-correlating pair Toto one. This exception occurs when you delete an entry for a "many" party.
"Concrete environment background" landlord is in the single table self-correlation (two-way pair of many to one) appears, the table details:

@Entity Public  class Department {    PrivateLong ID;Privateset<user> users =NewHashset<user> ();PrivateDepartment parent;Privateset<department> children =NewHashset<department> ();PrivateString name;PrivateString description;@Id    @GeneratedValue     PublicLonggetId() {returnId } Public void setId(Long ID) { This. id = ID; }@ManyToOne(Cascade=cascadetype.merge)@JoinColumn(name="parent_id") PublicDepartmentgetParent() {returnParent } Public void setParent(Department Parent) { This. parent = parent; }@OneToMany(mappedby="Parent", Cascade=cascadetype.all,fetch=fetchtype.eager) PublicSet<department>GetChildren() {returnChildren; } Public void Setchildren(Set<department> children) { This. Children = Children; }}

Visible is the self-Association of the Department department table, each department has a parent department and a subordinate department. The deletion of our ideal state is: Delete a department, 1, all its subordinate departments cascade Delete, 2, all the superior department does not have any changes.
"Solution!!!" " by the exception information: deleted object would is re-saved by the Cascade (remove deleted object from associations)--deleted objects will be cascaded the second time to save (Removes the object that has been deleted from the binding's contact). This means that we have done this, but because of the Cascade (Cascade), the deleted object has been exploited and so on ..., so the solution is to change the @onetomany cascade=cascadetype.all to cascade= Cascadetype.remove. Nothing else.
* * "Summary" **1, good at reading and thinking of abnormal content.
2, Cascade=cascadetype.all with caution, whether in the many side or one side.
3, of course, solid foundation is the key.

Hibernate:deleted object would is re-saved by cascade (remove deleted object from associations): []

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.