更新到 Hibernate Validator 4 后无法获取 Spring 的 DataIntegrityViolationException

发布于 2024-12-10 21:45:05 字数 315 浏览 0 评论 0原文

我不明白为什么从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哆兒滾 2024-12-17 21:45:06

这让我大吃一惊,但你是对的。

如果我关闭验证,我仍然会得到“验证”,并且不会得到 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 cause PropertyValueException, I get org.hibernate.exception.ConstraintViolationException, not even the expected javax.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.

扮仙女 2024-12-17 21:45:05

根据 JPA-2.0 规范,如果类路径中存在 JSR-303 验证器实现(即 Hibernate Validator 4.x),则在持久化实体之前会自动触发 JSR-303 验证。

因此,您的实体被 Hibernate Validator 拒绝,并且不会进入数据库,因此不会违反数据库完整性约束,并且不会引发 DataIntegrityViolationException ,并且您会得到 JSR-303 的 ConstraintViolationException代码> 代替。

来禁用此默认行为。

<validation-mode>NONE</validation-mode>

您可以通过添加到 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's ConstraintViolationException instead.

You can disable this default behaviour by adding

<validation-mode>NONE</validation-mode>

to your persistence.xml.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文