gpt4 book ai didi

java - Oracle/J2EE hibernate : ORA-00001: unique constraint violated due to HBM generated sequence

转载 作者:行者123 更新时间:2023-12-01 14:09:11 24 4
gpt4 key购买 nike

我总是收到约束错误

Caused by: java.sql.BatchUpdateException: ORA-00001: unique constraint violated

每当我尝试调用一个同步方法,该方法会在主键由 HBM 生成的表中插入一个值:

 <id name="logId" type="java.lang.Long">
<column name="LOG_ID" precision="20" scale="0" />
<generator class="sequence">
<param name="sequence">TRANS_LOG_ID</param>
<param name="allocationSize">100</param>
</generator>
</id>

这是线程调用的方法:

public synchronized static void saveTransLog(String detail, String stage) {

TransLog transLog = new TransLog();
transLog.setDetail(detail);
transLog.setStage(stage);

...

TransLogService.save(transLog);

}

我想知道对于此类多线程场景是否有更可靠的锁定处理。或者我需要添加一些内容才能解决我的约束违规问题。

非常感谢。

最佳答案

序列的当前值可能与行的主键冲突。您可以使用以下查询来验证这一点。

SELECT trans_log_id.curr_val FROM dual;

并将该数字与 LOG_ID 列的最大值进行比较。

这两个值应该相等或者序列的当前值应该更大。如果序列的当前值较小,请使用语句 trans_log_id.next_val 来增加序列的值。

关于java - Oracle/J2EE hibernate : ORA-00001: unique constraint violated due to HBM generated sequence,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18671556/

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