Cascade cascade of Hibernate and link maintenance Inverse

Source: Internet
Author: User

Hibernate Association, focusing on understanding cascade and Inverse

 

1. cascade is generally used for Cascade storage, cascade update, and cascade deletion.

 

1.1cascade annotation has two types, one is based on Hibernate Annotation

Org. hibernate. Annotations. Cascade

Org. hibernate. Annotations. cascadetype

Support cascade operations

All, persist // cascade persistence, call session. persist () will trigger the cascade event merge // cascade save or update, JPA specification hibernate to support added after jsr220, call the session. when Merge () is used, cascade remove is triggered. // cascade Delete is triggered. The JPA specification is the same as above. session is called. triggered when Delete ()
Refresh, delete, // cascade Delete, session. delete () triggers save_update, // cascade save or update session. save (), session. update (), session. saveorupdate (); replicate,

Configuration example

 

Java code
 
  1. @ Cascade (value = {org. hibernate. Annotations. cascadetype. All })
  2. Private studentinfo;
@ Cascade (value = {org. hibernate. Annotations. cascadetype. All}) Private studentinfo;

 

 

1.2 The second annotation is based on the JPA specification, that is, the Apache jsr220 specification, which is also the persistent layer specification of ejb3.

Javax. Persistence. cascadetype

All,

Persist: Calls session. persist () to trigger merge. Calls session. Merge () to trigger
Remove, triggered by calling session. Delete ()
Refresh, detach

Configuration example

 

Java code
 
  1. @ Manytoone (cascade = {cascadetype. Merge })
  2. @ Joincolumn (name = "teacher_id ")
  3. Private teacher;
@ Manytoone (cascade = {cascadetype. Merge}) @ joincolumn (name = "teacher_id") private teacher;

 

Cascade 1.3 is generally used on onetoone and onetoone, which is also officially recommended by hibernate. Sometimes we also use manytoone and manytoyun in development, just to write less code in testing, cascade other associated entities to persist an object,

As follows: Teacher and Student are manytoade, and the cascade annotation is added to facilitate testing.

 

Java code
 
  1. @ Test
  2. Public void addstudenttoteacher (){
  3. Student student1 = new student ("Zhang San", 20,200 72733l );
  4. Student student2 = new student ("Li Si", 20,200 72734l );
  5. Student student3 = new student ("Wang Wu", 20,200 72735l );
  6. Teacher = new teacher ("instructor Zhang ");
  7. Teacher. getstudents (). Add (student3 );
  8. Teacher. getstudents (). Add (student2 );
  9. Teacher. getstudents (). Add (student1 );
  10. This. teacherdao. Save (teacher );
  11. }
@ Testpublic void addstudenttoteacher () {student student1 = new student ("Zhang San", 20,200 72733l); Student student2 = new student ("Li Si", 20,200 72734l ); student student3 = new student ("Wang Wu", 20,200 72735l); Teacher = new teacher ("instructor Zhang"); teacher. getstudents (). add (student3); teacher. getstudents (). add (student2); teacher. getstudents (). add (student1); this. teacherdao. save (teacher );}

 

2. Inverse: reverse or reverse. It can be understood as control inversion, that is, who controls the relationship between entities, so inverse is used in object Association. Such as onetoone, oneto133, manytoyun

In onetoworkflow, if inverse is not specified, Hibernate will find the default table to maintain the relationship.

For example, we use the teacher and course entities to describe the relationship between teacher and course,

The configuration is as follows:

// Associate teacher with the foreign key teacher_id. Inverse uses mappedby to set

Java code
 
  1. @ Manytoone (cascade = {cascadetype. Merge })
  2. @ Joincolumn (name = "teacher_id ")
  3. Private teacher;
  4. @ Onetomany (mappedby = "teacher", fetch = fetchtype. Lazy, cascade = {cascadetype. merge, cascadetype. Remove })
  5. Private set <course> courses = new hashset <course> ();
@ Manytoone (cascade = {cascadetype. merge}) @ joincolumn (name = "teacher_id") private teacher; @ onetoworkflow (mappedby = "teacher", fetch = fetchtype. lazy, cascade = {cascadetype. merge, cascadetype. remove}) private set <course> courses = new hashset <course> ();

Here, the relationship between the courses attribute of the specified teacher is maintained by the teacher attribute of the associated entity course. If this attribute is not configured, The teacher_course table is used to maintain the relationship. Onetoone and manytoyun are similar, so I will not talk about them here!

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.