mercredi 1 avril 2015

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



@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?



@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():
}

Aucun commentaire:

Enregistrer un commentaire