在使用@ManyToMany时,若中间表只有相应的外键字段可以直接建立两个对应的Entity 设置ManyToMany

@ManyToMany 两个表多对多关联

但若是中间表有自己的附加字段,这需要为中间表建立Entity

具体如下:

Teacher <=> Student 中间表 teacher_student 附加字段 id(自增),siteId(站点id方便批删除)

增加 Teacher <=> Student 关联时,增加新的TeacherStudent

Entity:

@Entity
@Table(name = "teacher")
public class Teacher { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id; @Column(name = "name")
private String name; @Column(name = "age")
private int age; @ManyToMany(cascade = CascadeType.PERSIST, fetch=FetchType.LAZY)
@JoinTable(name="teacher_student",joinColumns={@JoinColumn(name="tid")},inverseJoinColumns={@JoinColumn(name="sid")})
private Set<Student> students; @OneToMany(mappedBy="teacher", fetch = FetchType.LAZY)
private Set<TeacherStudent> teacherStudents; 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;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public Set<Student> getStudents() {
return students;
} public void setStudents(Set<Student> students) {
this.students = students;
} public Set<TeacherStudent> getTeacherStudents() {
return teacherStudents;
} public void setTeacherStudents(Set<TeacherStudent> teacherStudents) {
this.teacherStudents = teacherStudents;
}
}
@Entity
@Table(name = "student")
public class Student { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id; @Column(name = "name")
private String name; @Column(name = "age")
private int age; @ManyToMany(cascade = CascadeType.PERSIST, fetch=FetchType.LAZY)
@JoinTable(name="teacher_student", joinColumns={@JoinColumn(name="sid")}, inverseJoinColumns={@JoinColumn(name="tid")})
private Set<Teacher> teachers; @OneToMany(mappedBy="student", fetch = FetchType.LAZY)
private Set<TeacherStudent> teacherStudents; 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;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public Set<Teacher> getTeachers() {
return teachers;
} public void setTeachers(Set<Teacher> teachers) {
this.teachers = teachers;
} public Set<TeacherStudent> getTeacherStudents() {
return teacherStudents;
} public void setTeacherStudents(Set<TeacherStudent> teacherStudents) {
this.teacherStudents = teacherStudents;
}
}

中间表

@Entity
@Table(name = "teacher_student")
public class TeacherStudent { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id; @ManyToOne(fetch = FetchType.LAZY)
@Cascade(value = CascadeType.SAVE_UPDATE)
@JoinColumn(name="tid")
private Teacher teacher; @ManyToOne(fetch = FetchType.LAZY)
@Cascade(value = CascadeType.SAVE_UPDATE)
@JoinColumn(name="sid")
private Student student; @JoinColumn
private int siteId; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public Teacher getTeacher() {
return teacher;
} public void setTeacher(Teacher teacher) {
this.teacher = teacher;
} public Student getStudent() {
return student;
} public void setStudent(Student student) {
this.student = student;
} public int getSiteId() {
return siteId;
} public void setSiteId(int siteId) {
this.siteId = siteId;
}
}
04-26 20:03