Bug 298550 - JPA 2 NOT IMPLEMENTED: @ManyToOne with optional=true
Summary: JPA 2 NOT IMPLEMENTED: @ManyToOne with optional=true
Status: NEW
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: All All
: P3 enhancement with 3 votes (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords: consistency
Depends on:
Blocks:
 
Reported: 2009-12-27 10:07 EST by Sam Halliday CLA
Modified: 2022-06-09 10:09 EDT (History)
7 users (show)

See Also:


Attachments
Failing test case - Netbeans Web App Project (25.69 KB, application/zip)
2009-12-27 10:13 EST, Sam Halliday CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Sam Halliday CLA 2009-12-27 10:07:00 EST
Build Identifier: 2.0.0

If one creates an @Entity with a @OneToMany mappedBy relationship with another @Entity, the elements of the collection can have the annotation (under JPA 1)

 @JoinColumn(nullable = false)

which means that the mappedBy column should never be null. In JPA 2, a new element to the @ManyToOne annotation was introduced - "optional". It should be imposing stricter checks on the relationship, ensuring that it always exists.

Unfortunately, EclipseLink allows this field/column to remain null regardless of which annotation type is used. I'm attaching a failing test case which shows the behaviour. The test case is a modification of the NetBeans Web JPA Sample and can be triggered by running and adding an entry.

Please consider that the test is failing in two places - to independently check your fixes, comment out individually the @JoinColumn(nullable = false) annotation, and the optional=true element.

I'm flagging this as a Major bug - it's broken JPA 1 and JPA 2 compatibility.

I discovered this bug when writing a testcase for a bug in Hibernate http://opensource.atlassian.com/projects/hibernate/browse/HHH-4740


Reproducible: Always

Steps to Reproduce:
Run the attached test case (coming soon)
Comment 1 Sam Halliday CLA 2009-12-27 10:10:12 EST
Apologies, the @JoinColumn(nullable = false) IS working - I'd forgotten to rebuild my schema ;-)

That makes this bug a JPA 2 incompatibility, not a JPA 1 regression.

Attachment coming next...
Comment 2 Sam Halliday CLA 2009-12-27 10:13:09 EST
Created attachment 155064 [details]
Failing test case - Netbeans Web App Project
Comment 3 Gordon Yorke CLA 2009-12-29 08:57:04 EST
The 'optional' attribute is present in all single value attributes.  It is however considered a hint and some providers may ignore 'optional'.  Also, not all providers may implement in the same way and depending on a particular provider's behaviour for 'optional' should be considered 'non-portable'.

What behaviour would you expect from EclipseLink?
Comment 4 Sam Halliday CLA 2009-12-29 09:33:20 EST
I cannot find any part of JPA 2 that says implementations may choose to ignore the "optional" element in @ManyToOne. The Javadocs certainly do not mention that this annotation element is considered a "hint"

http://java.sun.com/javaee/6/docs/api/javax/persistence/ManyToOne.html

Certainly the value of the element is (Optional), as are all the elements in the @OneToMany annotation, because they all have default values.

The behaviour I expect from EclipseLink is to implement this part of the JPA 2 standard - i.e. If set to false then a non-null relationship must always exist. One possible way to implement this would be to make it equivalent to "@JoinColumn(nullable = false)".

I would therefore expect some form of Exception to be raised when an operation is performed that breaks this constraint. The location or type of the Exception is irrelevant, so long as it is raised. Does EclipseLink currently have *any* defined behaviour?
Comment 5 Gordon Yorke CLA 2009-12-29 10:57:18 EST
Section 11.1.6 is the first mention of 'optional' and the spec states in text "The optional element is a hint as to whether the value of the field or property may be null."

Also the spec does not define any behaviour for this attribute and because the behaviour is undefined setting the 'optional' attribute must have provider specific behaviour which may not be portable (ie some providers could validate 'optional' when loading data from the database while others would consider that impractical)

@Column(nullable=flase) is better defined and has practical behaviour that is generally expected.

Currently EclipseLink ignores the 'optional' attribute.
Comment 6 Sam Halliday CLA 2009-12-29 11:27:57 EST
The part of the specification you refer to is specifically about the @Basic annotation. As you can see in the Javadocs

http://java.sun.com/javaee/6/docs/api/javax/persistence/Basic.html

this is clearly presented as a "hint" to users of the API.

Instead, go to section "11.1.26 ManyToOne Annotation" and note that there is no mention of "hint" for the "optional" element at all. In fact, the only element that is a "hint" is the LAZY fetch strategy.

Therefore, if EclipseLink does not do anything when the "optional" element is set to "false" and is violated, this is an incompatibility with the JPA 2 standard.

I am quite surprised that EclipseLink 2.0.0 passed the JPA 2 TCK - you might want to filter this bug up to the TCK maintainers in an effort to ensure that other implementations do not fail to implement this level of detail.
Comment 7 Florian Brunner CLA 2010-01-23 17:21:11 EST
+1

From the Javadoc I would also have expected this to work:

---

public abstract boolean optional

    (Optional) Whether the association is optional. If set to false then a non-null relationship must always exist.

    Default:
        true

---

http://java.sun.com/javaee/6/docs/api/javax/persistence/ManyToOne.html#optional%28%29


How can one see from the Javadoc, which attributes are non-portable?

Note: I dont think this i a new attribute in JPA 2.0, it already existed in 1.0:
http://www.oracle.com/technology/products/ias/toplink/jpa/resources/javadoc/javax/persistence/ManyToOne.html#optional__
Comment 8 Andreas Joseph Krogh CLA 2013-08-30 14:42:51 EDT
+1, this clarely is a bug in EL and should be fixed.
Comment 9 Eclipse Webmaster CLA 2022-06-09 10:03:18 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink
Comment 10 Eclipse Webmaster CLA 2022-06-09 10:09:16 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink