JSR-303和JPA重叠 [英] JSR-303 and JPA overlapping

查看:259
本文介绍了JSR-303和JPA重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Aloha嘿!

我已经使用了JPA注释,现在我也想使用JSR-303注释来验证属性。

I already use JPA annotations, now I'd like to use JSR-303 annotations to validate attributes as well.

我的问题是,他们彼此之间的互动情况如何?

如果我使用 @Basic(可选= false) @NotNull ,我还需要使用 nullable = false 等。 ?

My questions is, how well do they interact with each other?
If I use @Basic(optional=false) or @NotNull, will I still have to use nullable=false etc.?

我还没有决定使用哪种验证器实现,例如hibernate-validator或oVal(您也可以建议其他人。我知道 oVal不是JSR-303实现,但它映射了大多数注释)。

I haven't decided which validator implementation to use, e.g. hibernate-validator or oVal (You may also suggest others. I know that oVal is not a JSR-303 implementation, but it maps most annotations).

oVal手册(见上文)指出某些JPA注释也将被翻译成oVal注释。
Hibernate-validator 类似的机制

oVal manual (see above) states that certain JPA annotations will be translated into oVal annotations as well. Hibernate-validator has a similar mechanism.

有些搜索结果声称Bean Validation和JPA正式重叠,但我没有想出具体的方法。

Some search results claim that Bean Validation and JPA do officially overlap, but I have not figured in which exact way.

推荐答案

几乎完全阅读这两个规范之后,这就是我找到的。

After reading both specifications almost entirely, this is what I found.

我会自己回答我的问题;

截至目前;

I'll thusly answer my question myself;
As of now;


  • JPA和BV注释通常具有相同的含义。
    然而,BV约束的使用不会影响JPA实现的已创建DDL架构约束。

  • JPA and BV annotations do often share the same meaning. The usage of BV constraints however does not influence the created DDL schema constraints of the JPA implementation.

如果配置了JPA实现,则JPA实现将如此配置,在预先保留,预更新和预删除时检查BV约束。这是通过将工作委托给BV实现来完成的。另一方面,BV实现不会使用JPA约束。

The JPA implementation will, if configured so, check BV constraints at pre-persist, pre-update, and pre-remove. This is done by delegating the work to the BV implementation. The BV implementation on the other hand will not use JPA constraints.

使用BV约束,例如 @NotNull 自动导致JPA约束,例如 @Column(nullable = false)被推荐给BV规范中的JPA实现,但不是官方规范的一部分,因此不可靠行为。

Using BV constrains such as @NotNull to automatically cause JPA constraints such as @Column(nullable=false) is recommended to the JPA implementation in the BV specification, but not part of the official specification and thus not reliable behaviour.

因此,我必须同时使用BV和JPA注释,即使它们可能具有相同的意思。

As a result I will have to use both BV and JPA annotations, even though they may have the same meaning.

参考文献

  • Section 3.6 Bean Validation - JPA2 specification.
  • Appendix D Java Persistence 2.0 integration - Bean validation specification.

PS:我会等人是否有y更正或添加到此,然后设置为首选答案。

这篇关于JSR-303和JPA重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆