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
- @ Cascade (value = {org. hibernate. Annotations. cascadetype. All })
- 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
- @ Manytoone (cascade = {cascadetype. Merge })
- @ Joincolumn (name = "teacher_id ")
- 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
- @ Test
- Public 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 );
- }
@ 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
- @ Manytoone (cascade = {cascadetype. Merge })
- @ Joincolumn (name = "teacher_id ")
- Private teacher;
- @ Onetomany (mappedby = "teacher", fetch = fetchtype. Lazy, cascade = {cascadetype. merge, cascadetype. Remove })
- 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!