混乱:@NotNull vs @Column(nullable = false) [英] Confusion: @NotNull vs @Column(nullable = false)

查看:684
本文介绍了混乱:@NotNull vs @Column(nullable = false)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 当它们出现在 @Entity 的字段/ getter上时,它们之间有什么区别? (我通过 Hibernate 坚持实体)。

  2. 每个人都属于哪个框架和/或规范?

    >
  3. @NotNull 位于 javax.validation.constraints 。在 javax.validation.constraints.NotNull javadoc中,它表示

    lockquote

    带注释的元素不能为空


    但它并不涉及元素在数据库中的表示,所以为什么要添加约束 nullable = false 列?

  4. >

    @NotNull JSR 303 Bean验证注解。它与数据库约束本身无关。但是,由于Hibernate是JSR 303的参考实现,它会智能地提取这些约束条件并将它们转换为数据库约束条件,因此您可以获得两个约定的价格。 @Column(nullable = false ) 是JPA声明列非空的方式。即前者用于验证,后者用于指示数据库模式细节。您只需从Hibernate获得一些额外的(欢迎!)帮助即可。验证注释。


    1. When they appear on a field/getter of an @Entity, what is the difference between them? (I persist the Entity through Hibernate).

    2. What framework and/or specification each one of them belongs to?

    3. @NotNull is located within javax.validation.constraints. In the javax.validation.constraints.NotNull javadoc it says

      The annotated element must not be null

      but it does not speak of the element's representation in the database, so why would I add the constraint nullable=false to the column?

    解决方案

    @NotNull is a JSR 303 Bean Validation annotation. It has nothing to do with database constraints itself. As Hibernate is the reference implementation of JSR 303, however, it intelligently picks up on these constraints and translates them into database constraints for you, so you get two for the price of one. @Column(nullable = false) is the JPA way of declaring a column to be not-null. I.e. the former is intended for validation and the latter for indicating database schema details. You're just getting some extra (and welcome!) help from Hibernate on the validation annotations.

    这篇关于混乱:@NotNull vs @Column(nullable = false)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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