From 77aa1bbc5fcbb0cfb530c65cb57b2c8655173b00 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Wed, 31 Mar 2021 10:18:03 +0200 Subject: [PATCH] JpaPersistentPropertyImpl now considers super types association and target type. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The association target type detection in JpaPersistenPropertyImpl previously failed to honor the super class detected association target type as fallback. In fact, it didn't even override ….getAssociationTargetType() at all to expose the target type declared on JPA specific annotations. This is now in place and tested by verifying the AbstractPersistentProperty support for jMolecules annotations still work in case no JPA specific customizations are applied. Closes #2191 --- pom.xml | 8 ++++++++ .../mapping/JpaPersistentPropertyImpl.java | 20 +++++++++++++++++-- .../JpaPersistentPropertyImplUnitTests.java | 18 +++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index f2c031794..7c33148a2 100644 --- a/pom.xml +++ b/pom.xml @@ -243,6 +243,14 @@ ${querydsl} true + + + + org.jmolecules.integrations + jmolecules-spring + ${jmolecules-integration} + test + diff --git a/src/main/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImpl.java b/src/main/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImpl.java index f00b2910e..f6e2eab6b 100644 --- a/src/main/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImpl.java +++ b/src/main/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImpl.java @@ -101,7 +101,8 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty ASSOCIATION_ANNOTATIONS.stream().anyMatch(this::isAnnotationPresent)); + this.isAssociation = Lazy.of(() -> super.isAssociation() // + || ASSOCIATION_ANNOTATIONS.stream().anyMatch(this::isAnnotationPresent)); this.usePropertyAccess = detectPropertyAccess(); this.associationTargetType = detectAssociationTargetType(); this.updateable = detectUpdatability(); @@ -117,7 +118,10 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty getActualType() { - return associationTargetType != null ? associationTargetType.getType() : super.getActualType(); + + return associationTargetType != null // + ? associationTargetType.getType() // + : super.getActualType(); } /* @@ -213,6 +217,18 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty getAssociationTargetType() { + + return associationTargetType != null // + ? associationTargetType.getType() // + : super.getAssociationTargetType(); + } + /** * Looks up both Spring Data's and JPA's access type definition annotations on the property or type level to determine * the access type to be used. Will consider property-level annotations over type-level ones, favoring the Spring Data diff --git a/src/test/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImplUnitTests.java b/src/test/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImplUnitTests.java index b69b00f00..511136ec4 100644 --- a/src/test/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImplUnitTests.java +++ b/src/test/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImplUnitTests.java @@ -170,6 +170,16 @@ public class JpaPersistentPropertyImplUnitTests { assertThat(property.isAssociation()).isFalse(); } + @Test + void detectsJMoleculesAssociation() { + + JpaPersistentEntityImpl entity = context.getRequiredPersistentEntity(JMoleculesSample.class); + JpaPersistentProperty property = entity.getRequiredPersistentProperty("association"); + + assertThat(property.isAssociation()).isTrue(); + assertThat(property.getAssociationTargetType()).isEqualTo(JMoleculesAggregate.class); + } + private JpaPersistentProperty getProperty(Class ownerType, String propertyName) { JpaPersistentEntity entity = context.getRequiredPersistentEntity(ownerType); @@ -293,4 +303,12 @@ public class JpaPersistentPropertyImplUnitTests { @Column(updatable = false) String name; String updatable; } + + // jMolecules + + private static class JMoleculesSample { + Association association; + } + + private static interface JMoleculesAggregate extends AggregateRoot {} }