更新到 Hibernate Validator 4 后无法获取 Spring 的 DataIntegrityViolationException
我不明白为什么从 Hibernate Validator 3.X 升级到 4.X 后我不再收到 DataIntegrityViolationException 异常。
不知何故,Spring 无法再包装持久层异常。
除了 Validator 依赖项之外,没有任何更改,但验证在验证违规的情况下抛出 DataIntegrityViolationException 的测试不再通过。 我现在得到一个 javax.validation.ConstraintViolationException 。
一切都还在原地,当然包括 但翻译不再发生了。
欢迎帮助!
I can't understand why I'm not getting a DataIntegrityViolationException any more after upgrading from Hibernate Validator 3.X to 4.X.
Somehow Spring is not able to wrap the persistence layer exceptions anymore.
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.
help welcome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这让我大吃一惊,但你是对的。
如果我关闭验证,我仍然会得到“验证”,并且不会得到
DataIntegrityViolationException
根本原因PropertyValueException
,而是得到org.hibernate.exception.ConstraintViolationException< /code>,甚至不是预期的
javax.validation.ConstraintViolationException
所以看起来有 2 种验证启用休眠验证器。您似乎表明新 Bean 验证默认启用的是“预数据库”。
但无论如何,无论有或没有
NONE
,我的日志总是显示查询。可能是因为 Hibernate 日志记录的原因,它们无法进入数据库,但我有点不知道到底发生了什么。不使用
NONE
的验证和使用NONE
的验证有什么区别? AFAIK 文档中没有找到任何有关此内容的信息。It blows my mind, but you're kind of right.
If I turn off validation I still get the 'validation', and instead of getting the
DataIntegrityViolationException
root causePropertyValueException
, I getorg.hibernate.exception.ConstraintViolationException
, not even the expectedjavax.validation.ConstraintViolationException
So it looks like there's 2 kinds of validation enabled by Hibernate Validator. What you seem to indicate is that the one enabled by default by the new Bean Validation is 'pre database'.
But anyway, with or without
<validation-mode>NONE</validation-mode>
, my logs always shows the queries. Could be that they don't make it to the database because of Hibernate logging, but I'm kind of lost about what is happening really.What's the difference between the validation without
<validation-mode>NONE</validation-mode>
and the validation with. Haven't found anything about that in the doc AFAIK.根据 JPA-2.0 规范,如果类路径中存在 JSR-303 验证器实现(即 Hibernate Validator 4.x),则在持久化实体之前会自动触发 JSR-303 验证。
因此,您的实体被 Hibernate Validator 拒绝,并且不会进入数据库,因此不会违反数据库完整性约束,并且不会引发
DataIntegrityViolationException
,并且您会得到 JSR-303 的ConstraintViolationException
代码> 代替。来禁用此默认行为。
您可以通过添加到
persistence.xml
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.
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'sConstraintViolationException
instead.You can disable this default behaviour by adding
to your
persistence.xml
.