Hibernate Many-to-Many Unidirectional mapping

In many to many, it creates three tables in total. 

Student,Teacher,Student_teacher

In our example, all mappings are done in teacher class

 

1.Annotation

Teacher:

we create @ManyToMany @JoinTable(name="xx",@JoinColumns={@JoinColumn()},@inverseJoinColumns={@JoinColumn()})

@JoinColumns links its pk to the junction table

Why are we using @JoinColumns not @JoinColumn directly?

B/c we could have composite primary keys, thus, we should use @JoinColumns; 

@inverseJoinColumns links second table's pk to the junction table

@Entity
public 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;
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

Teacher class:

<Key> appears anywhere the parent mapping element defines a join to a new table that references the primary key of the original table. It also defines the foreign key in the joined table

<key column="xx"> xx is the id of this.class; 

<many-to-many class="student" column="xx"> xx is the id of student class

<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="stu_id"/>
    </set>
</class>



<hibernate-mapping package="com.hibernate.model">
<class name="Student" table ="S_Student">
   <id name="id"></id>
    <property name="name"></property>
    <property name="age"></property>
    <property name="classID"></property>
</class>

 

转载于:https://www.cnblogs.com/fifi043/p/4906183.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值