Repository: olingo-odata2 Updated Branches: refs/heads/master 2675f5f48 -> 2a22911dc
[OLINGO-824] Fixed JoinColumns on EDM creation Project: http://git-wip-us.apache.org/repos/asf/olingo-odata2/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata2/commit/a2f89adc Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata2/tree/a2f89adc Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata2/diff/a2f89adc Branch: refs/heads/master Commit: a2f89adc367e0f48d9dbdc8d24370dd053143cfa Parents: f8bbd74 Author: Michael Bolz <[email protected]> Authored: Mon Nov 16 19:27:22 2015 +0100 Committer: Michael Bolz <[email protected]> Committed: Mon Nov 16 19:27:22 2015 +0100 ---------------------------------------------------------------------- .../jpa/processor/core/model/JPAEdmFacets.java | 16 +++- .../processor/core/model/JPAEdmProperty.java | 87 ++++++++++---------- .../core/model/JPAEdmPropertyTest.java | 2 + .../jpa/processor/ref/model/Customer.java | 19 ++++- 4 files changed, 75 insertions(+), 49 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/a2f89adc/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmFacets.java ---------------------------------------------------------------------- diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmFacets.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmFacets.java index 51cd37b..04c795e 100644 --- a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmFacets.java +++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmFacets.java @@ -28,19 +28,26 @@ import org.apache.olingo.odata2.api.edm.provider.Facets; import org.apache.olingo.odata2.api.edm.provider.SimpleProperty; public class JPAEdmFacets { - public static void setFacets(final Attribute<?, ?> jpaAttribute, final SimpleProperty edmProperty) { + /** + * Create and set new facets for SimpleProperty. + * The new created and set facets are returned for further modification. + * + * @param jpaAttribute + * @param edmProperty property at which facets are set + * @return set new facets for SimpleProperty + */ + public static Facets createAndSet(final Attribute<?, ?> jpaAttribute, final SimpleProperty edmProperty) { EdmSimpleTypeKind edmTypeKind = edmProperty.getType(); Facets facets = new Facets(); edmProperty.setFacets(facets); Column column = null; if (jpaAttribute.getJavaMember() instanceof AnnotatedElement) { - column = ((AnnotatedElement) jpaAttribute - .getJavaMember()).getAnnotation(Column.class); + column = ((AnnotatedElement) jpaAttribute.getJavaMember()).getAnnotation(Column.class); } if (column == null) { - return; + return facets; } setNullable(column, edmProperty); @@ -68,6 +75,7 @@ public class JPAEdmFacets { default: break; } + return facets; } private static void setNullable(final Column column, final SimpleProperty edmProperty) { http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/a2f89adc/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmProperty.java ---------------------------------------------------------------------- diff --git a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmProperty.java b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmProperty.java index fbb8f96..a0168e9 100644 --- a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmProperty.java +++ b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmProperty.java @@ -39,6 +39,7 @@ import org.apache.olingo.odata2.api.edm.EdmSimpleTypeKind; import org.apache.olingo.odata2.api.edm.FullQualifiedName; import org.apache.olingo.odata2.api.edm.provider.ComplexProperty; import org.apache.olingo.odata2.api.edm.provider.ComplexType; +import org.apache.olingo.odata2.api.edm.provider.Facets; import org.apache.olingo.odata2.api.edm.provider.Property; import org.apache.olingo.odata2.api.edm.provider.SimpleProperty; import org.apache.olingo.odata2.jpa.processor.api.access.JPAEdmBuilder; @@ -192,13 +193,10 @@ public class JPAEdmProperty extends JPAEdmBaseViewImpl implements String targetEntityName = null; String entityTypeName = null; if (isBuildModeComplexType) { - jpaAttributes = sortInAscendingOrder(complexTypeView.getJPAEmbeddableType() - .getAttributes()); - entityTypeName = complexTypeView.getJPAEmbeddableType().getJavaType() - .getSimpleName(); + jpaAttributes = sortInAscendingOrder(complexTypeView.getJPAEmbeddableType().getAttributes()); + entityTypeName = complexTypeView.getJPAEmbeddableType().getJavaType().getSimpleName(); } else { - jpaAttributes = sortInAscendingOrder(entityTypeView.getJPAEntityType() - .getAttributes()); + jpaAttributes = sortInAscendingOrder(entityTypeView.getJPAEntityType().getAttributes()); entityTypeName = entityTypeView.getJPAEntityType().getName(); } @@ -210,13 +208,12 @@ public class JPAEdmProperty extends JPAEdmBaseViewImpl implements continue; } - PersistentAttributeType attributeType = currentAttribute - .getPersistentAttributeType(); + PersistentAttributeType attributeType = currentAttribute.getPersistentAttributeType(); switch (attributeType) { case BASIC: currentSimpleProperty = new SimpleProperty(); - properties.add(buildSimpleProperty(currentAttribute, currentSimpleProperty, false)); + properties.add(buildSimpleProperty(currentAttribute, currentSimpleProperty)); if (((SingularAttribute<?, ?>) currentAttribute).isId()) { if (keyView == null) { keyView = new JPAEdmKey(JPAEdmProperty.this); @@ -226,46 +223,37 @@ public class JPAEdmProperty extends JPAEdmBaseViewImpl implements } break; case EMBEDDED: - ComplexType complexType = complexTypeView - .searchEdmComplexType(currentAttribute.getJavaType().getName()); + ComplexType complexType = complexTypeView.searchEdmComplexType(currentAttribute.getJavaType().getName()); if (complexType == null) { - JPAEdmComplexTypeView complexTypeViewLocal = new JPAEdmComplexType( - schemaView, currentAttribute); + JPAEdmComplexTypeView complexTypeViewLocal = new JPAEdmComplexType(schemaView, currentAttribute); complexTypeViewLocal.getBuilder().build(); complexType = complexTypeViewLocal.getEdmComplexType(); complexTypeView.addJPAEdmCompleTypeView(complexTypeViewLocal); } - if (isBuildModeComplexType == false - && entityTypeView.getJPAEntityType().getIdType() - .getJavaType() - .equals(currentAttribute.getJavaType())) { + if (isBuildModeComplexType == false && entityTypeView.getJPAEntityType().getIdType().getJavaType() + .equals(currentAttribute.getJavaType())) { if (keyView == null) { - keyView = new JPAEdmKey(complexTypeView, - JPAEdmProperty.this); + keyView = new JPAEdmKey(complexTypeView, JPAEdmProperty.this); } keyView.getBuilder().build(); complexTypeView.expandEdmComplexType(complexType, properties, currentAttribute.getName()); } else { currentComplexProperty = new ComplexProperty(); if (isBuildModeComplexType) { - JPAEdmNameBuilder - .build((JPAEdmComplexPropertyView) JPAEdmProperty.this, - complexTypeView.getJPAEmbeddableType().getJavaType().getSimpleName()); + JPAEdmNameBuilder.build((JPAEdmComplexPropertyView) JPAEdmProperty.this, + complexTypeView.getJPAEmbeddableType().getJavaType().getSimpleName()); } else { JPAEdmNameBuilder - .build((JPAEdmComplexPropertyView) JPAEdmProperty.this, - JPAEdmProperty.this, skipDefaultNaming); + .build((JPAEdmComplexPropertyView) JPAEdmProperty.this, JPAEdmProperty.this, skipDefaultNaming); } - currentComplexProperty.setType(new FullQualifiedName( - schemaView.getEdmSchema().getNamespace(), - complexType.getName())); + currentComplexProperty + .setType(new FullQualifiedName(schemaView.getEdmSchema().getNamespace(), complexType.getName())); properties.add(currentComplexProperty); - if (!complexTypeView.isReferencedInKey(currentComplexProperty.getType().getName())) - { + if (!complexTypeView.isReferencedInKey(currentComplexProperty.getType().getName())) { complexTypeView.setReferencedInKey(currentComplexProperty.getType().getName()); } } @@ -276,8 +264,8 @@ public class JPAEdmProperty extends JPAEdmBaseViewImpl implements case ONE_TO_ONE: case MANY_TO_ONE: - if (attributeType.equals(PersistentAttributeType.MANY_TO_ONE) - || attributeType.equals(PersistentAttributeType.ONE_TO_ONE)) { + if (attributeType.equals(PersistentAttributeType.MANY_TO_ONE) || attributeType + .equals(PersistentAttributeType.ONE_TO_ONE)) { addForeignKey(currentAttribute); } @@ -292,11 +280,11 @@ public class JPAEdmProperty extends JPAEdmBaseViewImpl implements associationView.addJPAEdmAssociationView(associationViewLocal, associationEndView); } - if (attributeType.equals(PersistentAttributeType.MANY_TO_ONE) - || attributeType.equals(PersistentAttributeType.ONE_TO_ONE)) { + if (attributeType.equals(PersistentAttributeType.MANY_TO_ONE) || attributeType + .equals(PersistentAttributeType.ONE_TO_ONE)) { - JPAEdmReferentialConstraintView refConstraintView = new JPAEdmReferentialConstraint( - associationView, entityTypeView, JPAEdmProperty.this); + JPAEdmReferentialConstraintView refConstraintView = + new JPAEdmReferentialConstraint(associationView, entityTypeView, JPAEdmProperty.this); refConstraintView.getBuilder().build(); if (refConstraintView.isExists()) { @@ -309,8 +297,8 @@ public class JPAEdmProperty extends JPAEdmBaseViewImpl implements currentEntityName = entityTypeView.getJPAEntityType().getName(); if (currentAttribute.isCollection()) { - targetEntityName = ((PluralAttribute<?, ?, ?>) currentAttribute).getElementType().getJavaType() - .getSimpleName(); + targetEntityName = + ((PluralAttribute<?, ?, ?>) currentAttribute).getElementType().getJavaType().getSimpleName(); } else { targetEntityName = currentAttribute.getJavaType().getSimpleName(); } @@ -333,18 +321,25 @@ public class JPAEdmProperty extends JPAEdmBaseViewImpl implements } + private SimpleProperty buildSimpleProperty(final Attribute<?, ?> jpaAttribute, final SimpleProperty simpleProperty) + throws ODataJPAModelException, ODataJPARuntimeException { + return buildSimpleProperty(jpaAttribute, simpleProperty, null); + } + private SimpleProperty buildSimpleProperty(final Attribute<?, ?> jpaAttribute, final SimpleProperty simpleProperty, - final boolean isFK) - throws ODataJPAModelException, - ODataJPARuntimeException { + final JoinColumn joinColumn) + throws ODataJPAModelException, ODataJPARuntimeException { - JPAEdmNameBuilder - .build((JPAEdmPropertyView) JPAEdmProperty.this, isBuildModeComplexType, skipDefaultNaming, isFK); + boolean isForeignKey = joinColumn != null; + JPAEdmNameBuilder.build(JPAEdmProperty.this, isBuildModeComplexType, skipDefaultNaming, isForeignKey); EdmSimpleTypeKind simpleTypeKind = JPATypeConvertor .convertToEdmSimpleType(jpaAttribute .getJavaType(), jpaAttribute); simpleProperty.setType(simpleTypeKind); - JPAEdmFacets.setFacets(jpaAttribute, simpleProperty); + Facets facets = JPAEdmFacets.createAndSet(jpaAttribute, simpleProperty); + if(isForeignKey) { + facets.setNullable(joinColumn.nullable()); + } return simpleProperty; @@ -426,7 +421,11 @@ public class JPAEdmProperty extends JPAEdmBaseViewImpl implements } if (joinColumn.insertable() && joinColumn.updatable()) { currentSimpleProperty = new SimpleProperty(); - properties.add(buildSimpleProperty(currentRefAttribute, currentSimpleProperty, true)); + properties.add(buildSimpleProperty(currentRefAttribute, currentSimpleProperty, joinColumn)); + if(joinColumn.nullable()) { + currentSimpleProperty.getFacets(); + + } } } http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/a2f89adc/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmPropertyTest.java ---------------------------------------------------------------------- diff --git a/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmPropertyTest.java b/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmPropertyTest.java index f76a50f..c8e9fe1 100644 --- a/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmPropertyTest.java +++ b/odata2-jpa-processor/jpa-core/src/test/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmPropertyTest.java @@ -549,6 +549,7 @@ public class JPAEdmPropertyTest extends JPAEdmTestModelView { EasyMock.expect(joinColumn.referencedColumnName()).andReturn("SOLITID").anyTimes(); EasyMock.expect(joinColumn.insertable()).andReturn(true).anyTimes(); EasyMock.expect(joinColumn.updatable()).andReturn(true).anyTimes(); + EasyMock.expect(joinColumn.nullable()).andReturn(false).anyTimes(); EasyMock.replay(joinColumn); return (T) joinColumn; } else if (testCase.equals("NoJoinColumnNames")) { @@ -557,6 +558,7 @@ public class JPAEdmPropertyTest extends JPAEdmTestModelView { EasyMock.expect(joinColumn.referencedColumnName()).andReturn("").anyTimes(); EasyMock.expect(joinColumn.insertable()).andReturn(true).anyTimes(); EasyMock.expect(joinColumn.updatable()).andReturn(true).anyTimes(); + EasyMock.expect(joinColumn.nullable()).andReturn(true).anyTimes(); EasyMock.replay(joinColumn); return (T) joinColumn; } http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/a2f89adc/odata2-jpa-processor/jpa-ref/src/main/java/org/apache/olingo/odata2/jpa/processor/ref/model/Customer.java ---------------------------------------------------------------------- diff --git a/odata2-jpa-processor/jpa-ref/src/main/java/org/apache/olingo/odata2/jpa/processor/ref/model/Customer.java b/odata2-jpa-processor/jpa-ref/src/main/java/org/apache/olingo/odata2/jpa/processor/ref/model/Customer.java index 0605956..656b0f6 100644 --- a/odata2-jpa-processor/jpa-ref/src/main/java/org/apache/olingo/odata2/jpa/processor/ref/model/Customer.java +++ b/odata2-jpa-processor/jpa-ref/src/main/java/org/apache/olingo/odata2/jpa/processor/ref/model/Customer.java @@ -20,7 +20,9 @@ package org.apache.olingo.odata2.jpa.processor.ref.model; import java.sql.Timestamp; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; @@ -29,7 +31,10 @@ import javax.persistence.Entity; import javax.persistence.EntityListeners; import javax.persistence.EnumType; import javax.persistence.Enumerated; +import javax.persistence.FetchType; import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; @@ -39,9 +44,13 @@ import javax.persistence.Table; public class Customer extends CustomerBase { @Id - @Column(name = "ID") + @Column(name = "ID", nullable = false, length = 20) private Long id; + @ManyToOne(optional = true) + @JoinColumn(name = "PARENT_ID", referencedColumnName = "ID" , nullable = true) + private Customer parent; + @Column(name = "NAME") private String name; @@ -74,6 +83,14 @@ public class Customer extends CustomerBase { this.id = id; } + public Customer getParent() { + return parent; + } + + public void setParent(Customer parent) { + this.parent = parent; + } + public String getName() { return name; }
