chandransalem / odata4j

Automatically exported from code.google.com/p/odata4j
0 stars 0 forks source link

field always nullable when using JPAEdmGenerator with eclipselink #161

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Create an @Entity with @Column(nullable=false) (or even @Id)
2. Query $metadata of service using eclipselink as the JPA provider (issue does 
not apply to hibernate)
3. See that all fields are marked Nullable=true

What is the expected output? What do you see instead?

Nullable is true, which breaks clients such as LINQPad that validate that keys 
are not nullable.

What version of the product are you using? On what operating system?
Latest head version of odata4j; updated pom to use eclipselink 2.3.2 just to 
check if the issue still applies, and it does ..

Please provide any additional information below.

odata4j sets the EDM "Nullable" attribute based on 

http://docs.oracle.com/javaee/6/api/javax/persistence/metamodel/SingularAttribut
e.html#isOptional()

Eclipselink will return isOptional == true even when the column is not nullable 
(http://stackoverflow.com/questions/2899073/basicoptional-false-vs-columnnullabl
e-false-in-jpa).

My suggested "fix" is to check if nullable is set on the column attribute (if 
it's there), much like maxLength is based on this attribute a few lines later. 
Here's a patch that works for me:

diff -r 154b5d1a6abb 
odata4j-core/src/main/java/org/odata4j/producer/jpa/JPAEdmGenerator.java
--- 
a/odata4j-core/src/main/java/org/odata4j/producer/jpa/JPAEdmGenerator.java  Tue 
Apr 17 12:00:12 2012 -0600
+++ 
b/odata4j-core/src/main/java/org/odata4j/producer/jpa/JPAEdmGenerator.java  Fri 
Apr 20 11:43:46 2012 -0700
@@ -271,8 +271,14 @@
     Integer maxLength = null;
     if (sa.getJavaMember() instanceof AnnotatedElement) {
       Column col = ((AnnotatedElement) sa.getJavaMember()).getAnnotation(Column.class);
-      if (col != null && Enumerable.<EdmType> create(EdmSimpleType.BINARY, 
EdmSimpleType.STRING).contains(type))
-        maxLength = col.length();
+      if (col != null) {
+        if (Enumerable.<EdmType> create(EdmSimpleType.BINARY, 
EdmSimpleType.STRING).contains(type)) {
+          maxLength = col.length();
+        }
+        
+        // Force nullable to false if the column is not nullable
+        nullable &= col.nullable();
+      }
     }

     return EdmProperty.newBuilder(name).setType(type).setNullable(nullable).setMaxLength(maxLength);

Original issue reported on code.google.com by Charlie....@gmail.com on 20 Apr 2012 at 6:44

GoogleCodeExporter commented 8 years ago
Able to repro - will apply patch and add a test case.

Original comment by john.spurlock on 10 Nov 2012 at 9:03

GoogleCodeExporter commented 8 years ago
This issue was closed by revision b8abe54a7c61.

Original comment by john.spurlock on 10 Nov 2012 at 9:04

GoogleCodeExporter commented 8 years ago
rev link is broken .. it's revision 13d320065b8c

Original comment by Charlie....@gmail.com on 14 Oct 2013 at 9:12