java - 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:
解决方案
重要的部分是错误消息,它说ORA-00001: unique constraint (XXX.PK_USER_FAVORITE) violated
。显然,您的 USER_FAVORITE 表的唯一约束被违反了。
检查序列以查看其下一个值是多少,然后检查以查看 USER_FAVORITE 表中此唯一键列的最大值是多少。如果唯一列的最大值 >= 序列中的下一个值,则您必须解决问题。
推荐阅读
- javascript - 如何在 MongoDB 中保存多行代码片段,检索并以 HTML 格式显示它们
- c# - 获取随机n个数字的序列,这些数字加起来但仅来自一组特定的数字
- c# - 变量类型字典值的 C# 数据设计
- java - 在 ubuntu 上安装 java 13 返回 404 not found 错误
- sql-server - 实体框架 6 中是否识别 SQL Server 标量值?
- c - 为什么程序在相同条件下输出正确和错误的结果
- javascript - 无法将 Rasa 聊天机器人连接到 Flask 环境,因为我不断收到 404 错误,并且机器人卡在说“等待服务器”
- sql - 有没有办法仅使用 SQL 将表中的行打乱成独特的固定大小块?
- sql - 获取 SQL 中基于标志的 ID 的唯一元素的差异
- jquery - 检查输入文本字段是否具有特定的数值并使用 jquery 更改选择的选定选项