问题描述
-
当它们出现在
@Entity
的字段/获取器上时,它们之间有什么区别? (我通过休眠保留了实体).
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
位于javax.validation.constraints
中.在javax.validation.constraints.NotNull
javadoc中说
@NotNull
is located within javax.validation.constraints
. In the javax.validation.constraints.NotNull
javadoc it says
但是它没有说数据库中元素的表示形式,所以为什么要在列中添加约束nullable=false
?
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?
推荐答案
是 JSR 303 Bean验证注释.它与数据库约束本身无关.由于Hibernate是JSR 303的参考实现,因此它会智能地选择这些约束并将它们转换为数据库约束,因此您只需花1英镑即可获得2个. JPA的方法 @Column(nullable = false)
声明一列不为空. IE.前者用于验证,而后者则用于指示数据库架构详细信息.您将从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.
这篇关于困惑:使用JPA和Hibernate的@NotNull与@Column(nullable = false)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!