Hibernate's about many-to-many bidirectional associative mappings

Source: Internet
Author: User


"Hibernate" on Many-to-many bidirectional association mappings


Multi-to-many bidirectional association mappings in the project actual combat is still quite important, so here rewrite! Take the Student table (Student) and the teacher's Watch (Teacher) as an example.


First of all, we should look at annotations configuration first!


@Entity @table (name= "t_student") public class Student {    private Integer ID;    private String name;    Private Integer age;    Private set<teacher> teachers=new hashset<teacher> ()//Specify from student can also map teacher         @ManyToMany (mappedby= " Students ") Public    set<teacher>getteachers () {       returnteachers;    }    Publicvoidsetteachers (set<teacher> teachers) {       this.teachers = teachers;    }    @Id    @GeneratedValue public    Integer getId () {       returnid;    }    Publicvoid setId (integerid) {       this.id = ID;    }    @Column (name= "S_name") public    String GetName () {       returnname;    }    Publicvoid SetName (stringname) {       this.name = name;    }    @Column (name= "s_age") public    Integer Getage () {       returnage;    }    Publicvoid Setage (integerage) {       this.age = age;    }}

@Entity @table (name= "T_teacher") public class Teacher {    private Integer ID;    private String name;    Private set<student> students=new hashset<student> ();//set does not allow duplication, best suited for database model    @Id    @GeneratedValue Public    Integer getId () {       returnid;    }    Publicvoid setId (integerid) {       this.id = ID;    }    @Column (name= "T_name") public    String GetName () {       returnname;    }    Publicvoid SetName (stringname) {       this.name = name;    }    @ManyToMany    @JoinTable (name= "T_s_two",//Custom table name           joincolumns={@JoinColumn (name= "teacher_id")},//Custom column name           inversejoincolumns={@JoinColumn (name= "student_id")})//invert, and teacher the ID of the table corresponding to the custom public    set< Student>getstudents () {       returnstudents;    }    Publicvoidsetstudents (set<student> students) {       this.students = students;    }}

junittest Unit Test


@Test publicvoid Add () {try {configuration cfg=new configuration ();           Cfg.configure ();           Sessionfactory sessionfactory=cfg.buildsessionfactory ();           Sessionsession=sessionfactory.opensession ();           Session.begintransaction ();           Student s=new Student ();           S.setage (12);           S.setname ("Zhang San");           Session.save (s);           Student s2=new Student ();           S2.setage (13);           S2.setname ("John Doe");           Session.save (S2);           Teacher t=new Teacher ();           T.setname ("Miss Zhang");           Teacher t2=new Teacher ();           T2.setname ("Miss Li");           Set<student>students=newhashset<student> ();           Students.add (s);           Students.add (S2);         T.setstudents (students);//Set<teacher>teachers=new hashset<teacher> ();//Teachers.add (t);//           Teachers.add (T2);//S.setteacher (teachers);//S2.setteacher (teachers); Session. Save (t);           Session.save (T2);           Session.gettransaction (). commit ();       Sessionfactory.close ();       } catch (Hibernateexception e) {e.printstacktrace (); }    }

XML Configuration Methods


<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping public     "-//hibernate/hibernate mapping DTD 3.0//en"    "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">

<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping public     "-//hibernate/hibernate mapping DTD 3.0//en"    "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">

Ok, what you see is actually the value of the set inside the XML is copied, modified, it is important to note that

Both table and Cloum need to be consistent! This is the establishment of the intermediate table is normal!




Hibernate's about many-to-many bidirectional associative mappings

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.