gpt4 book ai didi

java - Springboot 2 CrudRepository.save总是抛出ConstraintViolationException

转载 作者:行者123 更新时间:2023-12-01 07:45:35 26 4
gpt4 key购买 nike

我已将项目中的 Springboot 版本从 1.4.3.RELEASE 迁移到 2.1.0.RELEASE。之后 CrudRepository.save() 总是抛出 org.hibernate.exception.ConstraintViolationException: 无法执行语句。

这是我在日志中看到的内容:

o.h.e.j.s.SqlExceptionHelper[m: SQL Error: 1062, SQLState: 23000
o.h.e.j.s.SqlExceptionHelper[m: Duplicate entry '11' for key 'PRIMARY'
o.h.i.ExceptionMapperStandardImpl[m: HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement

这是我试图保存的实体。

@Getter
@Setter
@Entity
@Table(name = "project_m")
public class Project {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false)
private Long id;

@Column(name = "name" , nullable = false)
private String name;

//other fields

}

最佳答案

从 Springboot 1.4.3.RELEASE 到 2.1.0.RELEASE 的变化是内部 Hibernate 版本从 5.05.3

其中发生的变化是 SequenceGenerator 的工作方式,该方式用于策略为 GenerationType.AUTO 的情况(如您的情况)。 enter image description here

链接到hibernate migration doc here .

有关 hibernate 生成策略的更多详细信息 here .

我的猜测是有 2 个并行 session 插入到该表中,并且它们现在共享序列号的本地副本,这会造成此冲突。但不确定!

我的建议是将策略更改为 GenerationType.SEQUENCE 并尝试。

关于java - Springboot 2 CrudRepository.save总是抛出ConstraintViolationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53265239/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com