我们正在为应用程序中的关键模块开发新的 API 服务,并使用 Spring boot 和 JPA 在这里,我们需要您的支持,因为我们面临一个有关 JPA 框架生成的序列的问题 生成的序列 ID 由其他服务器/实例并行共享,导致违反唯一约束
您能否就此提供任何建议以找到其根本原因,下面是我们在实体定义中使用的注释。
使用的注释是
@Id
@SequenceGenerator(name = "POT_PO_HEADER_S1", sequenceName = "POT_PO_HEADER_S1", allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "POT_PO_HEADER_S1")
@Column(name="POH_ID", unique=true, nullable=false, precision=22)
private Long pohId;
日志是
select
pot_po_header_s1.nextval
from
dual
2020-Feb-06 11:18:30.808 DEBUG [http-nio-8060-exec-1] o.h.i.e.SequenceStructure - Sequence value obtained: 14021954
2020-Feb-06 11:18:32.019 DEBUG [http-nio-8060-exec-1] o.hibernate.SQL -
insert
into
pot_po_header
(dept_code, dist_center, po_number, pot_cust_id, source_id, source_table, create_user, modify_user, create_user_name, modify_user_name, poh_id)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2020-Feb-06 11:18:32.673 DEBUG [http-nio-8060-exec-1] o.h.e.j.s.SqlExceptionHelper - could not execute statement [n/a]
java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (TNT_OWNER.POT_PO_HEADER_PK) violated
最佳答案
我不确定您如何获取在日志中打印的序列 ID,如下所示
2020-Feb-06 11:18:30.808 DEBUG [http-nio-8060-exec-1] o.h.i.e.SequenceStructure - 获得的序列值:14021954
保存实体后,您将获得序列值,如下所示
Entityr e = epository.save(entity);
然后您可以将 pohId
自动生成的值设置为
e.getPohId
关于java - 在 Spring boot 中使用 JPA 生成的序列 id(来自数据库序列)由其他服务器/实例共享,并导致违反唯一约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60107695/