1

我们在 Java 应用程序中使用 Hibernate。为了更新某条信息(及其所有相关的依赖项),我们使用了 Hibernate Merge 函数:

HibernateTemplate template = getHibernateTemplate();
template.deleteAll(toBeDeletedObjs);
Object mergedObj = template.merge(obj);

正在更新的主表有 5 列,其中第一列(PK)是自动生成的值。

有时,但并非总是如此,合并的 Insert 会出现以下错误:

[插入表(COL1、COL2、COL3、COL4、COL5)值(默认值、?、?、?、?)

DuplicateKeyException: One or more values in the INSERT statement, UPDATE statement, or foreign key update caused by a DELETE statement are not valid because the primary key, unique constraint or unique index identified by "ID" constrains table "TABLE" from having duplicate values for the index key.. SQLCODE=-803, SQLSTATE=23505, DRIVER=3.53.95

我假设“默认”是自动生成的 Hibernate 值,所以我没有检索前一个对象的 ID;这不可能是错误的原因。关于为什么 Hibernate 有时会在此合并中失败的任何想法?

4

0 回答 0