混乱:@NotNull vs @Column(nullable = false) [英] Confusion: @NotNull vs @Column(nullable = false)
问题描述
-
当它们出现在
@Entity
的字段/ getter上时,它们之间有什么区别? (我通过 Hibernate 坚持实体)。 每个人都属于哪个框架和/或规范? > -
@NotNull
位于javax.validation.constraints
。在javax.validation.constraints.NotNull
javadoc中,它表示
lockquote
带注释的元素不能为空
但它并不涉及元素在数据库中的表示,所以为什么要添加约束
nullable = false
列? > When they appear on a field/getter of an
@Entity
, what is the difference between them? (I persist the Entity through Hibernate).What framework and/or specification each one of them belongs to?
@NotNull
is located withinjavax.validation.constraints
. In thejavax.validation.constraints.NotNull
javadoc it saysThe 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
是 JSR 303 Bean验证注解。它与数据库约束本身无关。但是,由于Hibernate是JSR 303的参考实现,它会智能地提取这些约束条件并将它们转换为数据库约束条件,因此您可以获得两个约定的价格。 @Column(nullable = false )
是JPA声明列非空的方式。即前者用于验证,后者用于指示数据库模式细节。您只需从Hibernate获得一些额外的(欢迎!)帮助即可。验证注释。
@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屋!