我们如何解决 Spring Hibernate 中的 DataIntegrityViolationException?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4410087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can we solve the DataIntegrityViolationException in Spring Hibernate?
提问by Naresh
I am using Spring Hibernate with HibernateSupportDao, when I try to send multiple rows of data to store in my db, it raises a DataIntegrityViolationExceptionat a particular row. The same scenario has worked in remain in task on the same table. I do not get the problem even when I use the same code for the current task. Why was a DataIntegrityViolationExceptionthrown and how can I rectify that?
我将 Spring Hibernate 与 HibernateSupportDao 一起使用,当我尝试发送多行数据以存储在我的数据库中时,它会DataIntegrityViolationException在特定行引发 a 。在同一张桌子上的任务中保留了相同的场景。即使我对当前任务使用相同的代码,我也没有遇到问题。为什么被DataIntegrityViolationException抛出,我该如何纠正?
回答by hvgotcodes
Exception thrown when an attempt to insert or update data results in violation of an integrity constraint. Note that this is not purely a relational concept; unique primary keys are required by most database types.
当尝试插入或更新数据导致违反完整性约束时抛出异常。请注意,这不仅仅是一个关系概念;大多数数据库类型都需要唯一的主键。
Basically it means that hibernate tried to do something, but the database raised an exception. Most common case might be that you are deleting a parent but not deleting a child with a foreign key to that parent.
基本上这意味着休眠试图做某事,但数据库引发了异常。最常见的情况可能是您要删除父级,但未删除具有该父级外键的子级。
What are your transitive persistence (i.e. cascade) settings?
您的传递持久性(即级联)设置是什么?
回答by sampath
When you encounter this exception when selecting some fields from the table, then the reason could be the length of the one of the fields in the select statement. check for all the select field lengths and compare it with the table description. any field would have violated the field restriction.
当您在从表中选择某些字段时遇到此异常时,原因可能是 select 语句中字段之一的长度。检查所有选择的字段长度并将其与表描述进行比较。任何字段都会违反字段限制。
回答by Thirdman
DataIntegrityViolationException can also be violating a unique constraint on the database (i.e. a alternate key). Check, if your inserted data rows contain duplicate data that is violating such a unique constraint.
DataIntegrityViolationException 也可能违反数据库的唯一约束(即备用键)。检查您插入的数据行是否包含违反此类唯一约束的重复数据。
回答by Deep Shah
if you import database from somewhere then you have to take datafolder of server too , and change this datafolder to your server's data folder .
如果你从什么地方再导入数据库,你必须考虑数据服务器的文件夹过,并更改该数据文件夹到你的服务器的数据文件夹。
回答by Grigory Kislin
I treat DataIntegrityViolationException in ExceptionInfoHandler, finding DB constraints occurrences in root cause message and convert it into i18n message via map. See code here: https://stackoverflow.com/a/42422568/548473
我在 ExceptionInfoHandler 中处理 DataIntegrityViolationException,在根本原因消息中查找 DB 约束发生并通过映射将其转换为 i18n 消息。请参阅此处的代码:https: //stackoverflow.com/a/42422568/548473

