Spring批量迁移_seq表

zbdgwd5y  于 7个月前  发布在  Spring
关注(0)|答案(1)|浏览(68)

我正在从spring Boot 2迁移到spring 3的过程中,我遇到了Spring Batch的一个特殊问题。
我的源代码使用这种东西来生成密钥:

@Entity
public class Currency {
    @Id
    @GeneratedValue
    private Long id;

字符串
在我的MySQL数据库中,这些表是自动生成的:

| currency                     |
| currency_seq                 |


_seq表包含主键的值:

mysql> select * from currency_seq;
+----------+
| next_val |
+----------+
|       51 |
+----------+
1 row in set (0.00 sec)


现在使用Spring Batch 5,有些东西已经改变了,值被忽略了:

ConstraintViolationException: could not execute statement [Duplicate entry '1' for key 'currency.PRIMARY'] [insert into currency (currency,currency_change,date,id) values (?,?,?,?)]


我不知道该怎么办在这个migration guide
我用这些 prop :

spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.batch.jdbc.initialize-schema=ALWAYS

of1yzvn4

of1yzvn41#

使用@SequenceGenerator:

@Id
@SequenceGenerator(name = "currency_seq", sequenceName = "currency_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "currency_seq")
private Long id;

字符串
值大于1allocationSize不应用于高负载环境,因为它具有非易失性效应。

相关问题