Another talk about Hibernate cascade deletion--JPA hibernate implements a pair of cascade deletions Cascadetype.delete_orphan

Source: Internet
Author: User
Tags new set

Statement:
1, this article is original, not copied or reproduced over.
2, this thesis has done the experimental demonstration.
3. The hibernate configuration described in this article is based on annotations, and the HBM syntax is not available.
A lot of people are not clear about the persistence layer concept JPA, Hibernate, EJB3.0 relationship, here to do a simple explanation: JPA is a persistent layer design interface, EJB3.0 and Hibernate is a detailed implementation class, EJB3.0 and hibernate function approximation equal (Hibernate does not have a session bean,spring MVC3 Sessionattribute is similar to the session Bean).
The theory is that using the JPA interface can seamlessly switch the persistence layer implementation, but theoretically!!!


JPA is in hibernate mature and big line of time to launch, basically is to learn from Hibernate's strengths, do a unified standard just, JPA1.0 not a pair of cascade delete configuration, perhaps JPA2.0 in it (not done in the research)
@OneToMany (Mappedby = "Commentteam")
@Cascade ({Org.hibernate.annotations.cascadetype.save_update,org.hibernate.annotations.cascadetype.delete_ Orphan})
Private set<commentteammember> commentteammembers;
In order to illustrate, just put out a pair of key code, other irrelevant annotations have been ignored, so as not to cause interference.
Here are some highlights of the differences between the four frequently used annotation configurations:
Cascadetype.save_update
Cascadetype.all
Cascadetype.delete
Cascadetype.delete_orphan


All of these four are listed because I don't want to list all the concepts in the book. Basically 3 of the development time and enough to use, the following I combined with the code to demonstrate the differences between them, as well as the use of the attention of the place.
CascadeType.SAVE_UPDATE:Hibernate Proprietary, JPA does not support, the role is cascade Save, Cascade Update (Note: JPA is very disgusting, or you configure
Cascadetype.all, or you deserve cascadetype.save+casadetype.merge. Gossip: Experts, although the ox, for many years do not write code, set the standard let code trouble Ah! )
Cascadetype.all: Cascade Save, change, delete, synchronization, generally very little use, look at the console of a long list of SQL to know that performance is low, you have not changed the association table also send you an UPDATE statement, I never use this property.
Cascadetype.delete: When calling Session.delete (A), cascade deletes the associated object. (Note: Call A.SETB (NULL) First and then call Session.delete (A) so that cascade does not remove the B.
Cascadetype.delete_orphan: A pair of cascade deletions.


The following emphasis is on this cascadetype.delete_orphan:
Read the API, Development Guide, cascade Delete on a classic
@OneToMany (Mappedby = "Commentteam")
@Cascade ({Cascadetype.save_update,cascadetype.delete_orphan})
Private set<commentteammember> commentteammembers;


Mappedby, mapping a->b a pair of more than one side control reversal (who control who's problem), the new version of the Hibernate3.4 configuration more simple, changed a sentence, more concise it?
@OneToMany (Mappedby = "Commentteam", Orphanremoval=true)
Private set<commentteammember> commentteammembers;


The above two configuration methods are equivalent, the following is in the actual development of the use of, some time the code is not noticed, will mistakenly think clearly configured correctly, but why does not work? The following example looks at the code, see the action code (actually I am in spring controller, n years do not write DAO, the service is very small, manager side to go!) )
Commentteam Commentteam=this.gethibernatetemplate.get (Commentteam.class,id);
Commentteam.setcommentteammember (null);//want to cascade delete child table data
This.getHibernateTemplate.saveOrUpdate (Commentteam);
So cascade delete didn't happen? Why is it?
Another example.
Commentteam Commentteam=this.gethibernatetemplate.get (Commentteam.class,id);
Set<commentteammember> commentteammembers=new hashset<commentteammember> ();
Commentteam.setcommentteammember (commentteammembers);//want to cascade delete sub-table data or add or subtract replacement objects
This.getHibernateTemplate.saveOrUpdate (Commentteam);
This sample cascade delete effect did not happen! Even if the commentteammembers reason has several objects.


Syntax for successfully running the CASCADE Delete:
Commentteam Commentteam=this.gethibernatetemplate.get (Commentteam.class,id);
Commentteam.getcommentteammember (). Clear ();//Note the collection referenced here is also a collection of principles, there is no new
Commentteam.getcommentteammember (). Add (New Commentteammember ());//False assumption replaced with a new set can be used with the AddAll method
This.getHibernateTemplate.saveOrUpdate (Commentteam);

Analyze the reason: cascade Delete Works if the associated collection object cannot again point to the new reference, it must operate in the original collection, add, delete, empty elements, such as the above setxxx (null) method, such as the beginning to cascade delete effect, It is probably hibernate self-confessed to the collection of objects, their new put cascade delete Invalid!



Another talk about Hibernate cascade deletion--JPA hibernate implements a pair of cascade deletions Cascadetype.delete_orphan

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.