Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 334899 - Take into account optional attribute from @Basic or relationship tags for DDL generation
Summary: Take into account optional attribute from @Basic or relationship tags for DDL...
Status: NEW
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: Nobody - feel free to take it CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-01-20 10:26 EST by Daniel Le Berre CLA
Modified: 2022-06-09 10:24 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Daniel Le Berre CLA 2011-01-20 10:26:18 EST
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.
Comment 1 Daniel Le Berre CLA 2013-01-21 23:47:39 EST
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.
Comment 2 Joe Grassel CLA 2014-07-16 11:32:22 EDT
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.
Comment 3 Eclipse Webmaster CLA 2022-06-09 10:15:53 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink
Comment 4 Eclipse Webmaster CLA 2022-06-09 10:24:30 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink