What is the difference between @ManyToOne(optional=false) vs. @Column(nullable=false)

后端 未结 2 971
感情败类
感情败类 2020-12-22 19:08

In JPA, I am confused when to use the attribute optional=false and the annotation @Column(nullable=false). What is the difference?

2条回答
  •  心在旅途
    2020-12-22 19:36

    @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 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.)

提交回复
热议问题