更新到Hibernate Validator 4后无法获取Spring的DataIntegrityViolationException [英] Unable to get Spring's DataIntegrityViolationException after update to Hibernate Validator 4
问题描述
我不明白为什么从Hibernate Validator 3.X升级到4.X之后为什么再没有出现DataIntegrityViolationException.
I can't understand why I'm not getting a DataIntegrityViolationException any more after upgrading from Hibernate Validator 3.X to 4.X.
某种程度上,Spring不再能够包装持久层异常.
Somehow Spring is not able to wrap the persistence layer exceptions anymore.
除了Validator依赖项之外,没有其他任何更改,但是验证通过DataIntegrityViolationException在验证违反情况下引发的测试不再通过. 现在,我得到的是javax.validation.ConstraintViolationException.
Nothing has changed except the Validator dependency but the test validating that DataIntegrityViolationException is thrown in case of validation violation doesn't pass anymore. I now get a javax.validation.ConstraintViolationException instead.
一切仍然存在,当然包括 但是翻译不再发生.
Everything is still in place, including of course the but translation doesn't happen anymore.
欢迎光临!
推荐答案
根据JPA-2.0规范,如果JSR-303验证程序实现(即Hibernate Validator 4.x)为持久性,则在持久保存实体之前会自动触发JSR-303验证存在于类路径中.
According to JPA-2.0 specification, JSR-303 validation is automatically triggered before persisting an entity if JSR-303 validator implementation (i.e. Hibernate Validator 4.x) is present in the classpath.
因此,您的实体被Hibernate Validator拒绝并且不进入数据库,因此不会违反数据库完整性约束并且不会抛出DataIntegrityViolationException
,而是得到JSR-303的ConstraintViolationException
.
So, your entity is rejected by Hibernate Validator and don't get into the database, thus database integrity constraint is not violated and DataIntegrityViolationException
is not thrown, and you get JSR-303's ConstraintViolationException
instead.
您可以通过添加
<validation-mode>NONE</validation-mode>
到您的persistence.xml
.
这篇关于更新到Hibernate Validator 4后无法获取Spring的DataIntegrityViolationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!