某学生管理系统的3个表设计如下,并且已经在数据库中存在。
学生表:
课程表
关联表:
@Data
@Entity
@Table(name="student")
@JsonIgnoreProperties(value={"hibernateLazyInitializer"})
public class Student implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(nullable=false,length=16)
private String sid;
private String sname;
private String sex;
private int classId;
@ManyToMany
@JoinTable(
name="stu_cour", //中间表的名称
joinColumns = @JoinColumn(name="stu_id"),//中间表stu_cour字段关联student表的主键字段stu_id
inverseJoinColumns = @JoinColumn(name="cour_id")//中间表stu_cour字段关联cour表的主键字段cour_id
)
@JsonIgnore
private Set<Course> course;
}
@Data
@Entity
@Table(name="course")
@JsonIgnoreProperties(value={"hibernateLazyInitializer"})
public class Course implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String cname;
private int tutorId;
private String semester;
@ManyToMany(mappedBy= "courses") //表示该表为从表,关系由对方维护
@JsonIgnore
private Set<Student> students;
}