@OneToMany注解有mapperedBy字段
@ManyToOne注解没有mapperedBy字段
Student类
@Entity
@Table(name="student")
@Data
public class Student {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Column(name="name")
private String name;
@Column(name="age")
private Integer age;
//JoinColumn注解中name指的是实体对应表中的字段名,
//该字段对应的是Clazz类
@ManyToOne
@JoinColumn(name="yourClazz")
private Clazz myClazz;
}
Clazz类
@Entity
@Table(name="clazz")
@Data
public class Clazz {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Column(name="name")
private String name;
//mappedBy的值为Student类中 Clazz类型变量的变量名
@OneToMany(mappedBy="myClazz")
private Set<Student> students;
}