Community
Participate
Working Groups
It is currently possible to indicate in the object model that an attribute of the entity object is optional or not, using the @Basic or relationship annotations. @Basic(optional=false) int age; @ManyToOne(optional=false) Adress adress; Currently, that information is not taken into account during the DDL generation. To have a non null column in a database, one has to use the @Column or @JoinColumn nullable=false attribute : @Column(nullable=false) int age; @ManyToOne @JoinColumn(nullable=false) Adress adress; Since the aim of JPA is to gather as much information as possible from the object model, it should be nice to use the optional=false value in the DDL generation if it is not overriden by a Column or JoinColumn annotation.
Just a reminder. The issue still exists in EclipseLink 2.4.1. Here is what can be read on JPA Wikibook about the optional attribute: http://en.wikibooks.org/wiki/Java_Persistence/Basic_Attributes#Optional It is really an issue IMHO that the JPA 2.0 RI does not take into account that information during DDL generation.
For @Basic, the 2.1 spec describes optional with the following: "(Optional) Whether the value of the field or property may be null. This is a hint* and is disregarded for primitive types; it may be used in schema generation." So for primitive types (such as int), Eclipselink is not acting contrary to the specification. Considering that the @Column annotation also defines a "nullable" attribute, which at face value seems to do the same thing (for schema generation), it seems like there are two properties that accomplish the very same thing. Which begs the question as to the intent of the optional element, whether it should be a schema generation hint (and who wins, if it's not an error, when @Basic and @Column specify contrary values) or a runtime validation capability (which could conceivably be accomplished with Bean Validation, too.) The optional value with @ManyToOne**, as you pointed out, is pretty clear about the contract: "(Optional) Whether the association is optional. If set to false then a non-null relationship must always exist." -- the language there says nothing about it being a hint or a schema generation config element. * - The fact that it's also a hint also allows for it to be ignored, too. ** - And also @OneToOne.
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink