简体   繁体   English

一对一休眠-未分配ID

[英]Hibernate one to one unidirectional - ids not assigned

On my unit tests I get the JpaSystemException: ids for this class must be manually assigned before calling save() error . 在我的单元测试中,我得到了JpaSystemException: ids for this class must be manually assigned before calling save() error I have reworded my entities to make the question shorter and more readable. 我已经改写了我的实体,以使问题更短,更易读。

So student has an optional one to one relation with the table EnrolledInfo. 因此,student与表EnrolledInfo具有可选的一对一关系。

Student Entity 学生实体

@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 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..
}

Putting it together in a test (this errors) 将其放在一起进行测试(此错误)

Surely I dont have to save the enrolledInfo entity before I save the student one? 当然,在保存学生一个之前,我不必保存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():
}

Your EnrolledInfo has studentId as its Id, and no generation strategy cause its ids comes from the relationship as it is its parent id (the student). 您的EnrolledInfo的学生ID为id,并且没有任何生成策略会导致其ID来自关系,因为它是其父ID(学生)。 For that to work the student must be known during the save. 为此,必须在保存期间了解学生。

So the EnrolledInfo must know its student, jpa does not handle both sides of relationship itself, so they can be set manually. 因此,EnrolledInfo必须了解其学生,jpa本身并不处理关系的双方,因此可以手动设置它们。

  Student student = new Student();
  EnrolledInfo enrolledInfo = new EnrolledInfo(); 
  enrolledInfo.setPaidTuition(true);
  enrolledInfo.setStudent(student);

  student.setEnrolledInfo(enrolledInfo);        
  studentDao.save(student);

Now in JPA2, the proper annotation would be: 现在在JPA2中,正确的注释应为:

@Entity
@Table(name = "EnrolledInfo")
public class EnrolledInfo implements Serializable {

    @Id
    private Long studentId;

    @OneToOne(fetch = FetchType.Eager)
    @Id
    @JsonBackReference
    private Student student;

in JPA1 with @PrimaryKeyJoinColumn you would need to provide the full mappings (you should now use the JPA2, so there is no reason for the old one but just in case): 在带有@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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM