休眠一对一单向 - 未分配 ID
Hibernate one to one unidirectional - ids not assigned
在我的单元测试中,我得到 JpaSystemException: ids for this class must be manually assigned before calling save() error
。我改写了我的实体,使问题更短、更易读。
因此学生与 table EnrolledInfo 具有可选的一对一关系。
学生实体
@Entity
@Table(name = "Student")
public class Student {
@Id
@Column(name = "studentId")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long studentId;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "student", cascade = CascadeType.ALL)
private EnrolledInfo enrolledInfo;
private String firstName;
private String lastName;
// setters&getters etc..
}
EnrolledInfo 实体
@Entity
@Table(name = "EnrolledInfo")
public class EnrolledInfo implements Serializable {
@Id
private Long studentId;
@OneToOne(fetch = FetchType.Eager)
@PrimaryKeyJoinColumn
@JsonBackReference
private Student student;
@Column(name = "enrolledDate")
private Date enrolledDate;
@Column(name = "paidTuition")
private Boolean paidTuition;
// setters&getters etc..
}
把它放在一起测试(这个错误)
在保存学生之前我真的不需要保存 enrolledInfo 实体吗?
@Test
public void student_test() {
Student student = new Student();
EnrolledInfo enrolledInfo = new EnrolledInfo();
enrolledInfo.setPaidTuition(true);
student.setEnrolledInfo(enrolledInfo);
studentDao.save(student);
// errors on // JpaSystemException: ids for this class must be manually assigned before calling save():
}
您的 EnrolledInfo 将 studentId 作为其 Id,并且没有生成策略导致其 id 来自关系,因为它是其父 id(学生)。为此,必须在保存期间知道学生。
所以EnrolledInfo必须知道它的student,jpa本身不处理关系的两边,所以可以手动设置。
Student student = new Student();
EnrolledInfo enrolledInfo = new EnrolledInfo();
enrolledInfo.setPaidTuition(true);
enrolledInfo.setStudent(student);
student.setEnrolledInfo(enrolledInfo);
studentDao.save(student);
现在在 JPA2 中,正确的注释是:
@Entity
@Table(name = "EnrolledInfo")
public class EnrolledInfo implements Serializable {
@Id
private Long studentId;
@OneToOne(fetch = FetchType.Eager)
@Id
@JsonBackReference
private Student student;
在带有 @PrimaryKeyJoinColumn 的 JPA1 中,您需要提供完整的映射(您现在应该使用 JPA2,因此没有理由使用旧的,但以防万一):
@Entity
@Table(name = "EnrolledInfo")
public class EnrolledInfo implements Serializable {
@Id
@Column(name="STUDENT_ID")
private Long studentId;
@OneToOne(fetch = FetchType.Eager)
@PrimaryKeyJoinColumn(name="STUDENT_ID", referencedColumnName="STUDENT_ID")
@JsonBackReference
private Student student;
在我的单元测试中,我得到 JpaSystemException: ids for this class must be manually assigned before calling save() error
。我改写了我的实体,使问题更短、更易读。
因此学生与 table EnrolledInfo 具有可选的一对一关系。
学生实体
@Entity
@Table(name = "Student")
public class Student {
@Id
@Column(name = "studentId")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long studentId;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "student", cascade = CascadeType.ALL)
private EnrolledInfo enrolledInfo;
private String firstName;
private String lastName;
// setters&getters etc..
}
EnrolledInfo 实体
@Entity
@Table(name = "EnrolledInfo")
public class EnrolledInfo implements Serializable {
@Id
private Long studentId;
@OneToOne(fetch = FetchType.Eager)
@PrimaryKeyJoinColumn
@JsonBackReference
private Student student;
@Column(name = "enrolledDate")
private Date enrolledDate;
@Column(name = "paidTuition")
private Boolean paidTuition;
// setters&getters etc..
}
把它放在一起测试(这个错误)
在保存学生之前我真的不需要保存 enrolledInfo 实体吗?
@Test
public void student_test() {
Student student = new Student();
EnrolledInfo enrolledInfo = new EnrolledInfo();
enrolledInfo.setPaidTuition(true);
student.setEnrolledInfo(enrolledInfo);
studentDao.save(student);
// errors on // JpaSystemException: ids for this class must be manually assigned before calling save():
}
您的 EnrolledInfo 将 studentId 作为其 Id,并且没有生成策略导致其 id 来自关系,因为它是其父 id(学生)。为此,必须在保存期间知道学生。
所以EnrolledInfo必须知道它的student,jpa本身不处理关系的两边,所以可以手动设置。
Student student = new Student();
EnrolledInfo enrolledInfo = new EnrolledInfo();
enrolledInfo.setPaidTuition(true);
enrolledInfo.setStudent(student);
student.setEnrolledInfo(enrolledInfo);
studentDao.save(student);
现在在 JPA2 中,正确的注释是:
@Entity
@Table(name = "EnrolledInfo")
public class EnrolledInfo implements Serializable {
@Id
private Long studentId;
@OneToOne(fetch = FetchType.Eager)
@Id
@JsonBackReference
private Student student;
在带有 @PrimaryKeyJoinColumn 的 JPA1 中,您需要提供完整的映射(您现在应该使用 JPA2,因此没有理由使用旧的,但以防万一):
@Entity
@Table(name = "EnrolledInfo")
public class EnrolledInfo implements Serializable {
@Id
@Column(name="STUDENT_ID")
private Long studentId;
@OneToOne(fetch = FetchType.Eager)
@PrimaryKeyJoinColumn(name="STUDENT_ID", referencedColumnName="STUDENT_ID")
@JsonBackReference
private Student student;