I’ve bumped into this example in JPA 2.0 FR Specification, 11.1.37. OneToOne Annotation, page 403:
@OneToOne(optional=false)
@JoinColumn(name="CUSTREC_ID", unique=true, nullable=false, updatable=false)
public CustomerRecord getCustomerRecord() { return customerRecord; }
Is there any reason that I should put @OneToOne(optional=false) and at that same time put @JoinColumn(... nullable=false)?
Aren’t these two declarations the same? Isn’t one of them redundant?
Are both of them used in DDL schema generation?
Formally
optional=falseis a runtime instruction to the JPA implementation, andnullable=falseis an instruction to the DDL generator. So they are not strictly redundant.The difference can become significant when there is entity inheritance involved. If a particular mapping exists only on a subclass, and you have single table table per-hierarchy strategy, then the OneToOne mapping may be
optional=falseon the particular subclass that contains the mapping. However, the actual join column cannot be made not-null, since then other sub classes that share the table can’t be inserted!In practice different versions of different providers may or may not interpret either one at either time, caveat emptor.