首页 > 解决方案 > Hibernate 抛出托管刷新异常

问题描述

我们正在尝试在 Oracle 中保存实体:Oracle Database 11g Enterprise Edition Release 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 序列,这就是这里的主要气味。

我们试图保存在数据库上的实体也没有空值主键约束。

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

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

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

标签: javaoraclehibernatejpasequence

解决方案


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

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


推荐阅读