我使用Spring Boot 2创建微服务。我有一个拯救实体的方案。在entity for Id列中,我在
下面添加了@Id
@GeneratedValue(Strategy=GenerationType.Auto, generator="increment")
@GenericGenerator(name="increment", strategy="increment")
@Column(name="Id", insertable=false)
private Integer id;
上面的有时有效,有时会抛出主键约束冲突异常。但这并不一致。我将它作为两个具有不同端口的实例运行。
Error I get is unique constraint violation:
ConstraintViolationException: could not execute statement;约束(primary_key_cons);嵌套异常是ConstraintViolationException。
我唯一的选择是将策略更改为顺序。
是否手动插入了一些数据?也许hibernate正在生成id值已经存在于数据库中。如果可以,请清除该表并再次测试
不要使用带有增量策略的通用生成器。不建议在集群中使用。
增量生成long、short或int类型的标识符,这些标识符只有在没有其他进程向同一表中插入数据时才是唯一的。请勿在集群中使用
更多信息-点击这里
使用GenerationType.SEQUENCE
和crate aSequenceGenerator
。
,
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "YourSequenceGenerator")
@SequenceGenerator(sequenceName = "YourSequence", name = "YourSequenceGenerator", allocationSize = 1)
private Integer id;