尝试更新实体时为NullConstraint

时间:2019-06-12 13:44:13

标签: spring hibernate spring-boot spring-data-jpa spring-data

我有一个称为学期的实体。每学期包括几个讲座。因为我想使每个讲座都有一个等级,所以我不得不创建一个额外的实体,称为SemesterLecture。当我想要更新实体时,会收到以下错误代码:

org.postgresql.util.PSQLException:FEHLER:Spalte中的NULL Wert»semester_id«verletzt Not-Null-Constraint   详细信息:Fehlgeschlagene Zeileenthält(0,null,null)。

这是我更新学期实体的代码:

public Semester editSemester(Semester editSemester, long semesterId) throws NotFoundException {
    LOG.info("Change semester with id " + semesterId);
    if(semesterRepository.existsById(semesterId)){
        LOG.info("Found semester in database");
        Semester existingSemester = semesterRepository.findById(semesterId).get();
        for(SemesterLecture semesterLecture : editSemester.getSemesterLectures()){
            if(!existingSemester.getSemesterLectures().stream().filter(sl -> sl.getLecture().getId().equals(semesterLecture.getLecture().getId())).findFirst().isPresent()){
                LOG.info("Add new lecture...");
                Lecture selectedLecture = lectureRepository.findById(semesterLecture.getLecture().getId()).get();
                SemesterLecture createdSemesterLecture = new SemesterLecture(selectedLecture, semesterLecture.getGrade());
                createdSemesterLecture.setLecture(selectedLecture);
                createdSemesterLecture.setSemester(existingSemester);
                existingSemester.getSemesterLectures().add(createdSemesterLecture);
            }
        }
        LOG.info("Successfully updated all grades");
        return semesterRepository.save(existingSemester);
    }else {
        throw new NotFoundException("Semester with id " + semesterId + " does not exist");
    }
}




public class Semester {
@Id
@GeneratedValue(generator = "semester_generator")
@SequenceGenerator(
        name = "semester_generator",
        sequenceName = "semester_sequence",
        initialValue = 1000
)
private Long id;
private String title;
private int semesterNumber;
@ManyToOne
private Course course;
@OneToMany(mappedBy = "semester", cascade = CascadeType.ALL)
private List<SemesterLecture> semesterLectures = new ArrayList<>();



@Data
@Entity
public class Lecture {
@Id
@GeneratedValue(generator = "lecture_generator")
@SequenceGenerator(
        name = "lecture_generator",
        sequenceName = "lecture_sequence",
        initialValue = 1000
)
private Long id;
private String title;
private int credits;
private int sws;
private String modulNumber;
@OneToMany(mappedBy = "lecture", cascade = CascadeType.ALL)
@JsonIgnore
private List<SemesterLecture> semesterLectures = new ArrayList<>();



@Entity
public class SemesterLecture implements Serializable {
@Id
@ManyToOne
@JsonIgnore
@JoinColumn
private Semester semester;

@Id
@ManyToOne
@JoinColumn
private Lecture lecture;

private String grade;

public SemesterLecture(Lecture lecture, String grade) {
    this.lecture = lecture;
    this.grade = grade;
}

例如,此代码有效:

public Semester changeGradesOfSemesterLectures(long semesterId, Semester semester) throws NotFoundException {
    LOG.info("Change grades of semester with id " + semesterId);
    if(semesterRepository.existsById(semesterId)){
        LOG.info("Found semester in database");
        Semester existingSemester = semesterRepository.findById(semesterId).get();
        for(SemesterLecture semesterLecture : semester.getSemesterLectures()){
            for(SemesterLecture semesterLecture1 : existingSemester.getSemesterLectures()){
                if(semesterLecture.getLecture().getId().equals(semesterLecture1.getLecture().getId())){
                    LOG.info("Change grade of lecture with id " + semesterLecture1.getLecture().getId());
                    semesterLecture1.setGrade(semesterLecture.getGrade());
                }
            }
        }
        LOG.info("Successfully updated all grades");
        return semesterRepository.save(existingSemester);
    }else {
        throw new NotFoundException("Semester with id " + semesterId + " does not exist");
    }
}

0 个答案:

没有答案