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

后端 未结 2 972
感情败类
感情败类 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:35

    Both is used to prevent a null value, but if you mind that null should be blocked in ...

    The database layer (and you want to generate the schema using JPA) --> use @Column(nullable=false)

    The runtime (and before contacting the database)--> use optional=false (much faster than the first checking).

    If you want both abilities, use them both.

提交回复
热议问题