JPA Development Summary (5)-Multi-to-many bidirectional Association
Continue multi-to-many two-way associated learning, mainly analyzes the example of teachers and students. One teacher has multiple students and one student has multiple teachers, you can learn the one-to-many relationship to deal with it, and then repeat the precautions mentioned earlier. All the cited packages are javax. persistence, determine the relationship between the maintainer and so on. If you don't talk nonsense, you can directly read the code.
Teacher entity, students as relationship maintainers:
/*** Bidirectional many-to-many Association is a peering relationship, therefore, we define who serves as the maintenance end and the maintenance end * We choose students as the maintenance end of the relationship, in the future, only students can maintain the relationship between the two. * intermediate tables are required to store the definition of the relationship. */@ Entity @ Table (name = "t_teacher ") public class Teacher {@ Id @ GeneratedValue (strategy = GenerationType. AUTO) private Integer id; @ Column (length = 20, nullable = false) private String name; /*** do not use cascading delete in multiple-to-multiple relationships */@ ManyToMany (cascade = CascadeType. REFRESH, mappedBy = "teachers") private Set
Students; public Teacher () {} public Teacher (String name) {this. name = name ;}@ Overridepublic int hashCode () {final int prime = 31; int result = 1; result = prime * result + (id = null )? 0: id. hashCode (); result = prime * result + (name = null )? 0: name. hashCode (); result = prime * result + (students = null )? 0: students. hashCode (); return result ;}@ Overridepublic boolean equals (Object obj) {if (this = obj) return true; if (obj = null) return false; if (getClass ()! = Obj. getClass () return false; Teacher other = (Teacher) obj; if (id = null) {if (other. id! = Null) return false;} else if (! Id. equals (other. id) return false; if (name = null) {if (other. name! = Null) return false;} else if (! Name. equals (other. name) return false; if (students = null) {if (other. students! = Null) return false;} else if (! Students. equals (other. students) return false; return true ;}
}
Student Entity code:
/*** Student serves as the maintenance end for multiple-to-multiple relationships. The foreign key, intermediate table, and other information are defined in this class * to facilitate comparison of equal operations, which is useful in the remove Method, we override the hashcode and equals Methods */@ Entity @ Table (name = "t_student") public class Student {@ Id @ GeneratedValue (strategy = GenerationType. AUTO) private Integer id; @ Column (length = 20, nullable = false) private String name; /*** JoinTable indicates the intermediate table * inverseJoinColumns indicates the foreign key name of the intermediate table corresponding to the link maintenance end * joinColumns indicates the foreign key name of the intermediate table corresponding to the link maintenance end * @ return */@ manytoetype (cascade = {CascadeType. REFRESH}) @ JoinTable (name = "t_student_teacher", inverseJoinColumns = @ JoinColumn (name = "teacher_id"), joinColumns = @ JoinColumn (name = "student_id") private Set
Teachers;/*** the student is the maintainer and specifies the Add instructor Method * @ param teacher */public void addTeacher (Teacher teacher) {this. teachers. add (teacher);}/*** the student is the maintainer, And the instructor Removal Method * @ param teacher */public void removeTeacher (Teacher teacher) {if (this. teachers. contains (teacher) {this. teachers. remove (teacher) ;}} public Student () {}/ *** facilitates initialization. The minimum constructor * @ param name */public Student (String name) {this. name = name ;}@ Overridepublic int hashCo De () {final int prime = 31; int result = 1; result = prime * result + (id = null )? 0: id. hashCode (); result = prime * result + (name = null )? 0: name. hashCode (); result = prime * result + (teachers = null )? 0: teachers. hashCode (); return result ;}@ Overridepublic boolean equals (Object obj) {if (this = obj) return true; if (obj = null) return false; if (getClass ()! = Obj. getClass () return false; Student other = (Student) obj; if (id = null) {if (other. id! = Null) return false;} else if (! Id. equals (other. id) return false; if (name = null) {if (other. name! = Null) return false;} else if (! Name. equals (other. name) return false; if (teachers = null) {if (other. teachers! = Null) return false;} else if (! Teachers. equals (other. teachers) return false; return true ;}
}
Test code:
Public class ManyToMany {private static EntityManagerFactory factory; private static EntityManager em; @ BeforeClasspublic static void setUpBeforeClass () throws Exception {factory = Persistence. createEntityManagerFactory ("jpatest1"); em = factory. createEntityManager () ;}@ Testpublic void testSave () {em. getTransaction (). begin (); // start the transaction Student student1 = new Student ("Zhang San"); Student student2 = new Student ("Li Si "); teacher teacher1 = new Teacher ("instructor Wu"); Teacher = new Teacher ("instructor Zhang"); em. persist (student1); em. persist (teacher1); em. persist (student2); em. persist (teacher2); em. getTransaction (). commit (); // submit the transaction em. close (); // close factory. close (); // close}/*** establish the relationship between Teacher and Student */@ Testpublic void buildTS () {em. getTransaction (). begin (); // start the transaction // get the Student (because the student is the relationship maintenance end) // the Student is now in the managed status student Student = em. find (Student. class, 1); // establish a teacher-student relationship with Teacher Wu, because the teacher object has no specific operation, we leave student out. addTeacher (em. getReference (Teacher. class, 1); em. getTransaction (). commit (); // submit the transaction em. close (); // close factory. close (); // close}/*** disassociate Teacher from Student */@ Testpublic void removeTS () {em. getTransaction (). begin (); // start the transaction // get the Student (because the student is the relationship maintenance end) // the Student is now in the managed status student Student = em. find (Student. class, 1); // establish a teacher-student relationship with Teacher Wu, because the teacher object has no specific operation, we leave student out. removeTeacher (em. getReference (Teacher. class, 1); em. getTransaction (). commit (); // submit the transaction em. close (); // close factory. close (); // close }}Most recently, code pasting is used to facilitate future memories. It is convenient and quick to start with code ..