Skip to content

Commit 77aa1bb

Browse files
odrotbohmmp911de
authored andcommitted
JpaPersistentPropertyImpl now considers super types association and target type.
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
1 parent 1b7f5c4 commit 77aa1bb

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,14 @@
243243
<version>${querydsl}</version>
244244
<optional>true</optional>
245245
</dependency>
246+
247+
<!-- jMolecules -->
248+
<dependency>
249+
<groupId>org.jmolecules.integrations</groupId>
250+
<artifactId>jmolecules-spring</artifactId>
251+
<version>${jmolecules-integration}</version>
252+
<scope>test</scope>
253+
</dependency>
246254

247255
<!-- CDI -->
248256
<!-- Dependency order required to build against CDI 1.0 and test with CDI 2.0 -->

src/main/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImpl.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ public JpaPersistentPropertyImpl(JpaMetamodel metamodel, Property property,
101101

102102
Assert.notNull(metamodel, "Metamodel must not be null!");
103103

104-
this.isAssociation = Lazy.of(() -> ASSOCIATION_ANNOTATIONS.stream().anyMatch(this::isAnnotationPresent));
104+
this.isAssociation = Lazy.of(() -> super.isAssociation() //
105+
|| ASSOCIATION_ANNOTATIONS.stream().anyMatch(this::isAnnotationPresent));
105106
this.usePropertyAccess = detectPropertyAccess();
106107
this.associationTargetType = detectAssociationTargetType();
107108
this.updateable = detectUpdatability();
@@ -117,7 +118,10 @@ public JpaPersistentPropertyImpl(JpaMetamodel metamodel, Property property,
117118
*/
118119
@Override
119120
public Class<?> getActualType() {
120-
return associationTargetType != null ? associationTargetType.getType() : super.getActualType();
121+
122+
return associationTargetType != null //
123+
? associationTargetType.getType() //
124+
: super.getActualType();
121125
}
122126

123127
/*
@@ -213,6 +217,18 @@ public boolean isEmbeddable() {
213217
return isAnnotationPresent(Embedded.class) || hasActualTypeAnnotation(Embeddable.class);
214218
}
215219

220+
/*
221+
* (non-Javadoc)
222+
* @see org.springframework.data.mapping.model.AnnotationBasedPersistentProperty#getAssociationTargetType()
223+
*/
224+
@Override
225+
public Class<?> getAssociationTargetType() {
226+
227+
return associationTargetType != null //
228+
? associationTargetType.getType() //
229+
: super.getAssociationTargetType();
230+
}
231+
216232
/**
217233
* Looks up both Spring Data's and JPA's access type definition annotations on the property or type level to determine
218234
* the access type to be used. Will consider property-level annotations over type-level ones, favoring the Spring Data

src/test/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImplUnitTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,16 @@ void simplePropertyIsNotConsideredAnAssociation() {
170170
assertThat(property.isAssociation()).isFalse();
171171
}
172172

173+
@Test
174+
void detectsJMoleculesAssociation() {
175+
176+
JpaPersistentEntityImpl<?> entity = context.getRequiredPersistentEntity(JMoleculesSample.class);
177+
JpaPersistentProperty property = entity.getRequiredPersistentProperty("association");
178+
179+
assertThat(property.isAssociation()).isTrue();
180+
assertThat(property.getAssociationTargetType()).isEqualTo(JMoleculesAggregate.class);
181+
}
182+
173183
private JpaPersistentProperty getProperty(Class<?> ownerType, String propertyName) {
174184

175185
JpaPersistentEntity<?> entity = context.getRequiredPersistentEntity(ownerType);
@@ -293,4 +303,12 @@ private static class WithReadOnly {
293303
@Column(updatable = false) String name;
294304
String updatable;
295305
}
306+
307+
// jMolecules
308+
309+
private static class JMoleculesSample {
310+
Association<JMoleculesAggregate, Identifier> association;
311+
}
312+
313+
private static interface JMoleculesAggregate extends AggregateRoot<JMoleculesAggregate, Identifier> {}
296314
}

0 commit comments

Comments
 (0)