In JPA, I am confused when to use the attribute optional=false and the annotation @Column(nullable=false). What is the difference?
In JPA, I am confused when to use the attribute optional=false and the annotation
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
@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=falseis 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.)