首页 > Web开发 > 详细

hibernate(九)多对多关联

时间:2015-12-28 22:05:07      阅读:232      评论:0      收藏:0      [点我收藏+]

一、多对多单向关联

假设一个老师教多个学生,一个学生被多个老师教,这就是典型的多对多关系

配置方式是在Teacher类的getStudents()方法上添加注解@ManyToMany

@JoinTable(name="t_s" ,

joinColumns = { @JoinColumn(name="teacher_id")},

inverseJoinColumns = { @JoinColumn(name="student_id")}

)

Teacher类:

package cn.orlion.hibernate.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
@Entity
public class Teacher {

    private int id;
    
    private String name;
    
    private Set<Student> students = new HashSet<Student>();
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    @ManyToMany
    @JoinTable(name="t_s" , // t_s是生成的中间表的表名
        joinColumns = { @JoinColumn(name="teacher_id")},// teacher_id是生成中间表中参考自Teacher的外键
        inverseJoinColumns = { @JoinColumn(name="student_id")}// inverseJoinColumns配置的是反转的对象(即Student)的idstudent_id是中间表中参考自Student的外键
        )
    public Set<Student> getStudents() {
        return students;
    }

    public void setStudents(Set<Student> students) {
        this.students = students;
    }
    
}

Student类:

package cn.orlion.hibernate.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Student {

    private int id;
    
    private String name;
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

生成的sql语句:

create table Student (
        id integer not null auto_increment,
        name varchar(255),
        primary key (id)
    )

create table Teacher (
        id integer not null auto_increment,
        name varchar(255),
        primary key (id)
    )

create table t_s (
        teacher_id integer not null,
        student_id integer not null,
        primary key (teacher_id, student_id)
    )

alter table t_s 
        add constraint FK_3no0wm9bwtogr87t4gvd4t3x5 
        foreign key (student_id) 
        references Student (id)

alter table t_s 
        add constraint FK_pj2fmd6skwfi7jm2avlo4n6mf 
        foreign key (teacher_id) 
        references Teacher (id)

 

二、多对多双向关联

单向关联可一理解为:老师知道教的学生的名字,但是学生不知道教的多个老师。

双向关联可以理解为:老师知道教的学生的名字,学生也知道教的多个老师。

配置方式:Teacher类与单向关联相同,Student类添加Set<Teacher> teachers属性,在getTeaches()方法上添加注解@ManyToMany(mappedBy="students")

Teacher类:

package cn.orlion.hibernate.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
@Entity
public class Teacher {

    private int id;
    
    private String name;
    
    private Set<Student> students = new HashSet<Student>();
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    @ManyToMany
    @JoinTable(name="t_s" , // t_s是生成的中间表的表名
        joinColumns = { @JoinColumn(name="teacher_id")},
        inverseJoinColumns = { @JoinColumn(name="student_id")}
        )
    public Set<Student> getStudents() {
        return students;
    }

    public void setStudents(Set<Student> students) {
        this.students = students;
    }
    
}

Student类:

package cn.orlion.hibernate.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
@Entity
public class Student {

    private int id;
    
    private String name;
    
    private Set<Teacher> teachers = new HashSet<Teacher>();
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    @ManyToMany(mappedBy="students")
    public Set<Teacher> getTeachers() {
        return teachers;
    }

    public void setTeachers(Set<Teacher> teachers) {
        this.teachers = teachers;
    }
}

生成的sql语句:

create table Student (
        id integer not null auto_increment,
        name varchar(255),
        primary key (id)
    )

create table Teacher (
        id integer not null auto_increment,
        name varchar(255),
        primary key (id)
    )

create table t_s (
        teacher_id integer not null,
        student_id integer not null,
        primary key (teacher_id, student_id)
    )

alter table t_s 
        add constraint FK_3no0wm9bwtogr87t4gvd4t3x5 
        foreign key (student_id) 
        references Student (id)

alter table t_s 
        add constraint FK_pj2fmd6skwfi7jm2avlo4n6mf 
        foreign key (teacher_id) 
        references Teacher (id)

 

hibernate(九)多对多关联

原文:http://www.cnblogs.com/orlion/p/5083748.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!