Hibernate 正在抛出托管刷新异常

Hibernate is throwing managed flush exception

我们正在尝试在 Oracle 中保存实体:Oracle 数据库 11g企业版 11.2.0.3.0

我们正在使用 Hibernate 同样:

我们收到以下明显错误:

o.h.i.ExceptionMapperStandardImpl : HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]

详细的相关堆栈跟踪如下:

[ERROR]--- [nio-8080-exec-9] o.h.engine.jdbc.spi.SqlExceptionHelper : ORA-00001: unique constraint (XXX.PK_USER_FAVORITE) violated
 [WARN ]  --- [nio-8080-exec-9] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1, SQLState: 23000
[ERROR] --- [nio-8080-exec-9] o.h.i.ExceptionMapperStandardImpl : HHH000346: Error during managed flush [**org.hibernate.exception.ConstraintViolationException**: could not execute statement]
[ERROR] --- [nio-8080-exec-9] c.mastercard.refarch.aop.ServiceLogging : {"aop_service":"UserFavoritesService", "method":"addCurrentUserFavorite", "type":"failed", "error":"could not execute statement; SQL [n/a]; constraint [XXX.PK_USER_FAVORITE]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement", "args":"(["s029158",{"userFavoriteId":null,"applicationId":"XXX","functionAlphaCode":"SOME vale","title":"Service Report"}])"}

我们正在使用 oracle 序列 来生成主键值。我想当 oracle 序列试图执行时我们遇到了上述问题。 每当我们遇到上述异常时,Hibernate 都无法执行 Oracle 序列,这是这里的主要问题。

还有 not nullprimary key constraint 我们试图保存在 DB 上的实体。

当前的 Hibernate 版本是 Hibernate-core 5.2.17 和版本,如果休眠 JPA 是 Hibernate-JPA-2.1-API-1.0.2.Final

仍然想知道为什么我们得到:

管理刷新期间出错 [org.hibernate.exception.ConstraintViolationException:

重要的部分是错误消息 ORA-00001: unique constraint (XXX.PK_USER_FAVORITE) violated。显然违反了 USER_FAVORITE table 的唯一约束。

检查序列以查看其下一个值是什么,然后检查 USER_FAVORITE table 中此唯一键列的最大值是多少。如果唯一列的最大值 >= 序列中的下一个值,则您必须解决问题。