@Basic(optional = false) vs @Column(nullable = false) in JPA

java, jpa, orm

Solution

Gordon Yorke (EclipseLink Architecture Committee Member, TopLink Core Technical Lead, JPA 2.0 Expert Group Member) wrote a good answer on this topic so instead of paraphrasing him, I'll quote his answer:

The difference between `optional` and `nullable` is the scope at which they are evaluated. The definition of '`optional`' talks about property and field values and suggests that this feature should be evaluated within the runtime. '`nullable`' is only in reference to database columns.

If an implementation chooses to implement `optional` then those properties should be evaluated in memory by the Persistence Provider and an exception raised before SQL is sent to the database otherwise when using '`updatable=false`' '`optional`' violations would never be reported.

Problem

What's the difference between `@Basic(optional = false)` and `@Column(nullable = false)` in JPA persistence?

Original source

Related problems