班级和学生是一对多的关联关系,由学生维护(学生表引用了班级表的主键作为外键)
班级
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column
private String className;
@OneToMany(mappedBy="classRoom")//另外一方(学生)维护,有外键的一方维护
private Set<Student> students;
学生
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column
private String name;
@ManyToOne(fetch=FetchType.LAZY)//多的一方延迟加载
@JoinColumn(name="classroom_id")//添加外键
private ClassRoom classRoom;