Hibernate Many-to-Many Bidirectional mapping

來源:互聯網
上載者:User

標籤:

1.Annotation

@Entitypublic class Teacher {    private String name;    private int id;    private Set<Student> students;    @ManyToMany    @JoinTable(name="t_s",    //join column points to this.id;        joinColumns={@JoinColumn(name="teacher_id")},        //inverseJoinColumns points to id of student class        inverseJoinColumns={@JoinColumn(name="stu_id")}        )    public Set<Student> getStudents() {        return students;    }    public void setStudents(Set<Student> students) {        this.students = students;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @Id    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }}@Entity@Table(name="t_student")public class Student {    private int age;    private int classID;    private String name;    private int id;    private Set<Teacher> teachers;        @ManyToMany(mappedBy="students")    public Set<Teacher> getTeachers() {        return teachers;    }    public void setTeachers(Set<Teacher> teachers) {        this.teachers = teachers;    }    public int getClassID() {        return classID;    }        public void setClassID(int classID) {        this.classID = classID;    }        public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }        public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }        @Id    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }}

 

2.XML

Junction table‘s name and FK ,PK must match to each other!!

<hibernate-mapping package="com.hibernate.model"><class name="Teacher" table="T_Teacher">   <id name="id"></id>    <property name="name"></property>    <!-- table="xx" xx is name of junction table -->    <set name="students" table="tea_stu">      <key column="teacher_Id"></key>      <many-to-many class="com.hibernate.model.Student" column="student_id"/>    </set></class><class name="Student" table ="S_Student">   <id name="id"></id>    <property name="name"></property>    <property name="age"></property>    <property name="classID"></property>    <set name="teachers" table="tea_stu">      <key column="student_Id"></key>      <many-to-many class="com.hibernate.model.Student" column="teacher_id"/>    </set></class>

 

Hibernate Many-to-Many Bidirectional mapping

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.