本文介绍了org.springframework.dao.DataIntegrityViolationException错误报告原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我使用hibernate插入到所有列被定义为非空的mysql表中。它有一个独特的主键和几列上的另一个唯一索引。 我得到以下错误: org.springframework.dao.DataIntegrityViolationException:无法执行JDBC批更新; SQL [insert into MY_TABLE(col1,col2,col3, col4,ID_)values(?,?,?,?,?)];约束[null] 这个错误是在客户日志中,我不能自己再现问题,所以我不能放入调试以查看插入语句中的值。 我的理解是,constraint [null]意味着违反了非空约束。然而,看看我的代码,我看不出任何可能的方式,任何数据在插入时都可能为空,除非hibernate试图插入空ID(这在hibernate中是一个非常糟糕的错误,所以看起来好像不太可能)。 然而,我可以看到如何发生一个独特的约束被违反。信息有可能是误导性的,我实际上得到了唯一的密钥违规?如果您搜索构造函数的调用者,那么constraint [null]是否总是意味着违反了非空约束?解析方案 在Spring源代码中的DataIntegrityViolationException中,您会发现它在 org.springframework.orm.hibernate3.SessionFactoryUtils 中调用: return new DataIntegrityViolationException(ex.getMessage()+; SQL [+ jdbcEx.getSQL()+ ]; constraint [+ jdbcEx.getConstraintName( )+],ex); 所以这个异常是由违反约束引起的, null 是由JDBC异常返回的约束名称。因此,您应该责怪MySQL驱动程序未在JDBC异常中填充违反的约束名称。但违反的约束可能是任何约束,并不一定是 not null 约束。 I'm using hibernate to insert to a mysql table on which all columns are defined as not null. It has a unique primary key and another unique index on several columns.I'm getting the following error: org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; SQL [insert into MY_TABLE(col1, col2, col3, col4, ID_) values (?, ?, ?, ?, ?)]; constraint [null]This error is in customer logs and I can't reproduce the problem myself, so I can't put in debugging to see what the values are in the insert statement.My understanding is that "constraint [null]" means a "not null" constraint is being violated. However, looking at my code, I cannot see any possible way that any of the data could be null at the time of the insert, unless hibernate is trying to insert a null ID (which would be a very bad bug in hibernate and so seems unlikely).However, I can see how it could happen that a unique constraint is being violated. Is it possible that the message is misleading and I'm actually getting a unique key violation? Does "constraint[null]" always mean a not null constraint was violated? 解决方案 If you search for the callers of the constructor of DataIntegrityViolationException in the Spring source code, you'll find that it's called in org.springframework.orm.hibernate3.SessionFactoryUtils:return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]; constraint [" + jdbcEx.getConstraintName() + "]", ex);So the exception is caused by a violated constraint, and null is the name of the constraint as returned by the JDBC exception. So you should blame the MySQL driver for not populating the violated constraint name in the JDBC exception. But the violated constraint could be any constraint, and not necessarily a not null constraint. 这篇关于org.springframework.dao.DataIntegrityViolationException错误报告原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-24 13:08