问题描述
在JPA中,我很困惑何时使用属性 optional = false 和注释 @Column(nullable = false)。有什么区别?
In JPA, I am confused when to use the attribute optional=false and the annotation @Column(nullable=false). What is the difference?
推荐答案
@Column(nullable = false)是用于生成模式的指令。在实际数据库中,生成在类外的数据库列将被标记为不可空。
@Column(nullable=false) is an instruction for generating the schema. The database column generated off the class will be marked not nullable in the actual database.
可选= false 是运行时指令。它所做的主要功能与延迟加载有关。除非你记得设置optional = false(因为Hibernate不知道应该有一个代理还是一个null,除非你告诉它空值是不可能的,所以它可以产生一个非集合映射实体一个代理。)
optional=false is a runtime instruction. The primary functional thing it does is related to Lazy Loading. You can't lazy load a non-collection mapped entity unless you remember to set optional=false (because Hibernate doesn't know if there should be a proxy there or a null, unless you tell it nulls are impossible, so it can generate a proxy.)
这篇关于@ManyToOne(可选= false)和@Column(nullable = false)有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!