如何解决尝试保存对象时 - 对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例
我在尝试保存成绩对象时遇到 TransientPropertyValueException。在这种情况下,学生可能包含已经存在的成绩,以及刚刚由 UI 创建的成绩。有趣的是,对于一些学生,成绩被成功保存,而对于其他学生 - 没有。这是我的代码。我想知道是不是因为所有的级联而不是双向的?
@Entity
@Table(uniqueConstraints = {
@UniqueConstraint(columnNames = "phoneNumber"),@UniqueConstraint(columnNames = "email"),@UniqueConstraint(columnNames = "ucn")})
public class Student extends Person {
@NotBlank
@Size(min = 10,max = 20)
private String phoneNumber;
@NotBlank
@Email
@Size(max = 50)
private String email;
@NotBlank
private String country;
@NotBlank
private String address;
@NotBlank
private String citizenship;
@NotBlank
@Size(min = 10,max = 14)
private String ucn;
@OnetoOne
private Parent mother;
@OnetoOne
private Parent father;
@OnetoOne
private Doctor doctor;
@OnetoMany(cascade = CascadeType.ALL)
private Set<Note> notes;
@OnetoMany(cascade = CascadeType.ALL)
private Set<Grade> grades;
@OnetoMany(cascade = CascadeType.ALL)
private Set<Absence> absences;
}
@Entity
public class Grade {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Date date;
@NotNull
private float mark;
private String description;
@OnetoOne
private Teacher byWhom;
@OnetoOne
private Subject subject;
在学生服务中:
public StudentDto save(final StudentDto studentDto) {
log.info("Saving new student: " + studentDto.toString());
Student beforeSaving = modelmapper.map(studentDto,Student.class);
final Parent mother = modelmapper.map(studentDto.getMother(),Parent.class);
final Parent father = modelmapper.map(studentDto.getFather(),Parent.class);
final Doctor doctor = modelmapper.map(studentDto.getDoctor(),Doctor.class);
beforeSaving.setMother(mother);
beforeSaving.setFather(father);
beforeSaving.setDoctor(doctor);
final Student student = repository.save(beforeSaving);
return modelmapper.map(student,StudentDto.class);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。