JPA 中的 @Basic(optional = false) 与 @Column(nullable = false) [英] @Basic(optional = false) vs @Column(nullable = false) in JPA
问题描述
JPA持久化中@Basic(optional = false)
和@Column(nullable = false)
的区别是什么?
What's the difference between @Basic(optional = false)
and @Column(nullable = false)
in JPA persistence?
推荐答案
Gordon Yorke(EclipseLink 架构委员会成员、TopLink 核心技术负责人、JPA 2.0 专家组成员)就这个主题写了一个很好的答案,所以我没有转述他,而是转述他将引用他的回答:
Gordon Yorke (EclipseLink Architecture Committee Member, TopLink Core Technical Lead, JPA 2.0 Expert Group Member) wrote a good answer on this topic so instead of paraphrasing him, I'll quote his answer:
optional
和nullable
是它们的作用域被评估.的定义'optional
' 谈论财产和字段值并建议这特征应该在运行.'nullable
' 仅在对数据库列的引用.
The difference between
optional
andnullable
is the scope at which they are evaluated. The definition of 'optional
' talks about property and field values and suggests that this feature should be evaluated within the runtime. 'nullable
' is only in reference to database columns.
如果一个实现选择实现 optional
然后那些属性应该在持久化提供者的内存和在发送 SQL 之前引发的异常否则使用时到数据库'updatable=false
' '可选
'永远不会报告违规行为.
If an implementation chooses to
implement optional
then those
properties should be evaluated in
memory by the Persistence Provider and
an exception raised before SQL is sent
to the database otherwise when using
'updatable=false
' 'optional
'
violations would never be reported.
这篇关于JPA 中的 @Basic(optional = false) 与 @Column(nullable = false)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!