列“ object_service_id”中的空值违反了非空约束

时间:2019-04-02 06:48:54

标签: java spring-boot

objectServiceRepository.save(objectService);

        if (request.getObjectServiceJobs() != null) {
            if (objectService.getObjectServiceJobs() != null) {
                objectService.getObjectServiceJobs().forEach(job -> {
                    objectServiceJobRepository.delete(job);
                });
            }
            List<ObjectServiceJob> objectServiceJobs = new ArrayList<>();
            request.getObjectServiceJobs().forEach(serviceJob -> {
                ObjectServiceJob objectServiceJob = new ObjectServiceJob();
                objectServiceJob.setObjectService(objectService);
                objectServiceJob.setJob(serviceJob.getJob());
                objectServiceJob.setWorkDetail(serviceJob.getWorkDetail());
                objectServiceJobRepository.save(objectServiceJob);
            });
            objectService.setObjectServiceJobs(objectServiceJobs);
        }

return objectServiceRepository.save(objectService);

仅在编辑ObjectService时出现约束错误。添加新的时,一切正常。

ERROR: null value in column "object_service_id" violates not-null constraint
Detail: Failing row contains (16, null, 1, 1).

通过“详细信息”部分,我认为在将ObjectService设置为ObjectServiceJob时遇到问题。添加新的ObjectService时,我使用:

public ObjectService createObjectService(PatchObjectServiceRequest request){
        return this.setObjectServiceData(request, new ObjectService());
    }

在修改ObjectService时,我使用:

public ObjectService modifyObjectService(PatchObjectServiceRequest request, Long id){
        return this.setObjectServiceData(request, getObjectServiceById(id));
    }

奇怪的是更改已经完成,但是我仍然收到代码500错误。

ObjectService实体:

@Entity
@Getter
@Setter
@Table(name = "object_service")
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="id")
public class ObjectService {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(unique = true, nullable = false, columnDefinition = "serial")
    private Long id;

    @Column(name = "size")
    private int size;

    @Column(name = "est_time_in_min")
    private int estTimeInMinutes;

    @Column(name = "requierment")
    private int requierment;

    @Column(name = "note")
    private String note;

    @Column(name = "on_holidays")
    private boolean onHolidays;

    @Column(name = "is_billable")
    private boolean isBillable;

    @Column(name = "done_weekly")
    private boolean doneWeekly;

    @Column(name = "status", nullable = false, columnDefinition = "active_status")
    @Enumerated(EnumType.STRING)
    @Type(type = "pgsql_enum")
    private User.UserStatus status;

    @ManyToOne
    @JoinColumn(name = "object_id", nullable = false)
    private Object object;

    @ManyToOne
    @JoinColumn(name = "service_id", nullable = false)
    private Service service;

    @OneToMany
    @JoinColumn(name = "object_service_id")
    private List<ObjectServiceJob> objectServiceJobs;

    @ManyToOne
    @JoinColumn(name = "day_schedule_id")
    private DaySchedule daySchedule;

    @ManyToOne
    @JoinColumn(name = "week_schedule_id")
    private WeekSchedule weekSchedule;

    @Column(name = "create_time", nullable = false, updatable = false)
    @JsonIgnore
    private Date createTime = new Date();

    @Column(name = "update_time", nullable = false)
    @JsonIgnore
    private Date updateTime = new Date();

    @PreUpdate
    @PrePersist
    public void onCreateOnUpdate() {
        updateTime = new Date();
    }
}

0 个答案:

没有答案