@ManyToOne(optional=false) 和@Column(nullable=false) 有什么区别 [英] What is the difference between @ManyToOne(optional=false) vs. @Column(nullable=false)
问题描述
在 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.
optional=false
是运行时指令.它所做的主要功能与延迟加载有关.你不能延迟加载一个非集合映射实体,除非你记得设置 optional=false(因为 Hibernate 不知道那里是否应该有一个代理或一个空值,除非你告诉它空值是不可能的,所以它可以生成一个代理.)
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(optional=false) 和@Column(nullable=false) 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!