第一种方式(manytomany):
@Entity
@Table(name="t_admin")
public class Admin {
private int id;
private String name;
private Set<Role> roles;
public Admin() {
roles=new HashSet<>();
}
public void add(Role role) {
roles.add(role);
}
@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="admins")
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
}
第二种方式:
@Entity
@Table(name="t_role")
public class Role {
private int id;
private String name;
private Set<Admin> admins;
public Role() {
admins=new HashSet<>();
}
public void add(Admin admin) {
admins.add(admin);
}
@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_admin_role",joinColumns= {@JoinColumn(name="r_id")},inverseJoinColumns= {@JoinColumn(name="a_id")})
public Set<Admin> getAdmins() {
return admins;
}
public void setAdmins(Set<Admin> admins) {
this.admins = admins;
}
}
@JoinTable(name="t_admin_role",joinColumns= {@JoinColumn(name="r_id")},inverseJoinColumns= {@JoinColumn(name="a_id")})
jointable表示关联表,,joinColumns= {@JoinColumn(name="r_id")}表示该属于该类的外键在关联表的名称,inverseJoinColumns= {@JoinColumn(name="a_id")})表示另一个类的外键在关联表的名称
然后在另一个类也要加入@ManyToMany(mappedBy="admins"),表示由对立类的admins属性维护关系
第二种方式(两个manytoone):
@Entity
public class TeacherCourse {
private int id;
private double ach;
private Teacher teacher;
private Course course;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getAch() {
return ach;
}
public void setAch(double ach) {
this.ach = ach;
}
@ManyToOne
@JoinColumn(name="t_id")
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
@ManyToOne
@JoinColumn(name="c_id")
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
}
@Entity
public class Teacher {
private int id;
private String name;
private Set<TeacherCourse> tcs;
@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;
}
@OneToMany(mappedBy="teacher")
@LazyCollection(LazyCollectionOption.EXTRA)
public Set<TeacherCourse> getTcs() {
return tcs;
}
public void setTcs(Set<TeacherCourse> tcs) {
this.tcs = tcs;
}
}
@Entity
public class Course {
private int id;
private String name;
private Set<TeacherCourse> tcs;
@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;
}
@OneToMany(mappedBy="course")
@LazyCollection(LazyCollectionOption.EXTRA)
public Set<TeacherCourse> getTcs() {
return tcs;
}
public void setTcs(Set<TeacherCourse> tcs) {
this.tcs = tcs;
}
}