Hibernate-----多对多关联映射(注解)

多对多映射采取中间表连接的映射策略,建立的中间表将分别引入两边的主键作为外键

Many

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import org.hibernate.annotations.GenericGenerator;
@Entity
public class Teacher implements java.io.Serializable {
    private Integer teacherid;
    private String name;
 
    private Set<Student> students = new HashSet<Student>();
 
    @Id
    @GeneratedValue(generator = "TeacherGenerator")
    @GenericGenerator(name = "TeacherGenerator", strategy = "native")
    public Integer getTeacherid() {
       return teacherid;
    }
 
    public void setTeacherid(Integer teacherid) {
       this.teacherid = teacherid;
    }
 
    @Column(name = "name", nullable = false, length = 32)
    public String getName() {
       return name;
    }
 
    public void setName(String name) {
       this.name = name;
    }
 
    // @ManyToMany表示Teacher是多对多关系的一端
    // @JoinTable描述了多对多关系的数据表关系: name(中间表名称),joinColumns(中间表与Teacher表的外键关系),
    // inverseJoinColumns(中间表与另外一端(Student)的外键关系)
    // 中间表的Teacher_ID外键指向Teacher的主键teacherid
    // 中间表的Student_ID外键指向Student的主键studentid
    @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
    @JoinTable(name = "Teacher_Student",
               joinColumns = { @JoinColumn(name = "Teacher_ID", referencedColumnName = "teacherid") },
              inverseJoinColumns = { @JoinColumn(name = "Student_ID", referencedColumnName = "studentid") })
    public Set<Student> getStudents() {
       return students;
    }
 
    public void setStudents(Set<Student> students) {
       this.students = students;
    }
}

Many

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
@Entity
public class Student implements java.io.Serializable {
    private Integer studentid;
    private String name;
 
    private Set<Teacher> teachers = new HashSet<Teacher>();
 
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Integer getStudentid() {
       return studentid;
    }
 
    public void setStudentid(Integer studentid) {
       this.studentid = studentid;
    }
 
    @Column(name = "student_name", nullable = false, length = 32)
    public String getName() {
       return name;
    }
 
    public void setName(String name) {
       this.name = name;
    }
 
    // @ManyToMany表示Student是多对多关系的一边
    // 使用mappedBy属性将维护端交由另外一端(Teacher)
    @ManyToMany(mappedBy = "students")
    public Set<Teacher> getTeachers() {
       return teachers;
    }
 
    public void setTeachers(Set<Teacher> teachers) {
       this.teachers = teachers;
    }
}



删除学生,因为学生不是关系维护者,所以需先手工解除老师与学生的关联,然后再删除学生
删除老师,因为老师是关系维护者,所以可直接解除老师与学生的关系,不用手工解除



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。