尝试保存对象时 - 对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例

问题描述

我在尝试保存成绩对象时遇到 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 (将#修改为@)