带有昨天日期的Spring Data更新实体

时间:2019-07-28 12:08:34

标签: java mysql spring date jpa

我在将数据更新(并插入)到数据库时遇到了问题。我有具有某些integer属性,某些String属性的实体,但是还有一个类型为LocalDate的属性,它必须为unique

我在数据库中放入了很多类似的实体,但是用户需要对其进行编辑并更新一些属性。当我尝试对其进行测试并更改一些String属性并将更新的实体保存到数据库时,我在控制台中看到此错误日志:

Duplicate entry '2019-07-27' for key 'work_day_workday_date_uindex'

如您所见,Hibernate尝试将对象与昨天的日期放在一起。但为什么?在将对象保存到数据库之前,我通过输入System.out.println指令以传统(:D)->的方式对其进行了检查。 日志显示了正确的打印日期: WorkDay{id=296, date=2019-07-28, workingTime=8,.... 因此,我认为问题与数据库和应用程序之间的时间差异有关。

我在StackOverflow中找到了一些技巧。有人说在SpringBoot中从serverTimezone=UTC中删除application.properties可能会有所帮助。它解决了问题-昨天我成功更新了实体。但是今天我回到编码,问题再次出现。

我希望也许你们中的某些人过去曾经遇到过这个问题,并且知道一些解决方案-这对我非常有帮助:)

这是工作日实体:

@Entity
@Table(name = "work_day")
public class WorkDay implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_workday")
    private Long id;
    @NotNull
    @Column(name = "workday_date", nullable = false, unique = true)
    private LocalDate date;
    @NotNull
    @Column(name = "working_time", nullable = false)
    private Integer workingTime;
    @Column(name = "booked_artist")
    private String bookedArtist;

    @ManyToOne
    @JoinColumn(name="workday_importance_id")
    private WorkDayImportance workDayImportance;

    @ManyToMany
    @JoinTable(name = "workday_employee",
            joinColumns = {@JoinColumn(name = "workday_id",
                    referencedColumnName = "id_workday")},
            inverseJoinColumns = {@JoinColumn(name="employee_id",
                    referencedColumnName = "id_employee")})
    private List<Employee> employers;

    @OneToMany(mappedBy = "workDay", cascade = CascadeType.REMOVE)
    private List<Comment> comments;

以下是我执行此操作的一些代码:

    public void setBookedArtist(Long workDayId, String artist){
            workDayRepository
                 .findById(workDayId)
                 .ifPresent(workDay -> workDayDetailsService.saveBookedArtist(workDay, artist));
        }

    void saveBookedArtist(WorkDay workDay, String artist){
            if(artist != null && !artist.equals("")) {
                workDay.setBookedArtist(artist);
                workDayRepository.save(workDay);
            }
        }

实体存储库是扩展JpaRepository的Spring Data接口。

最诚挚的问候!

1 个答案:

答案 0 :(得分:1)

在保存记录之前设置workDay的ID应该可以,并且由于我们不想更新以下设置的updatable = false日期

 public void setBookedArtist(Long workDayId, String artist){
            workDayRepository
                 .findById(workDayId)
                 .ifPresent(workDay -> workDayDetailsService.saveBookedArtist(workDay, artist));
        }

    void saveBookedArtist(WorkDay workDay, String artist){
            if(artist != null && !artist.equals("")) {
                workDay.setId(workDay.getId());
                workDay.setBookedArtist(artist);
                workDayRepository.save(workDay);
            }
        }

 @NotNull
    @Column(name = "workday_date", nullable = false, unique = true, updatable = false)
    private LocalDate date;