Skip to content

Commit c7c2df6

Browse files
odrotbohmmp911de
authored andcommitted
Support for jMolecules' @Identity annotation.
Fixes #2438 Original pull request: #2439.
1 parent 23ff807 commit c7c2df6

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.springframework.data.util.ClassTypeInformation;
4141
import org.springframework.data.util.Lazy;
4242
import org.springframework.data.util.Optionals;
43+
import org.springframework.data.util.ReflectionUtils;
4344
import org.springframework.data.util.StreamUtils;
4445
import org.springframework.data.util.TypeInformation;
4546
import org.springframework.lang.Nullable;
@@ -56,6 +57,7 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp
5657
extends AbstractPersistentProperty<P> {
5758

5859
private static final String SPRING_DATA_PACKAGE = "org.springframework.data";
60+
private static final Class<? extends Annotation> IDENTITY_TYPE = loadIdentityType();
5961

6062
private final @Nullable String value;
6163
private final Map<Class<? extends Annotation>, Optional<? extends Annotation>> annotationCache = new ConcurrentHashMap<>();
@@ -74,7 +76,8 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp
7476
.of(() -> !isTransient() && !isAnnotationPresent(ReadOnlyProperty.class));
7577
private final Lazy<Boolean> isReference = Lazy.of(() -> !isTransient() //
7678
&& (isAnnotationPresent(Reference.class) || super.isAssociation()));
77-
private final Lazy<Boolean> isId = Lazy.of(() -> isAnnotationPresent(Id.class));
79+
private final Lazy<Boolean> isId = Lazy
80+
.of(() -> isAnnotationPresent(Id.class) || IDENTITY_TYPE != null && isAnnotationPresent(IDENTITY_TYPE));
7881
private final Lazy<Boolean> isVersion = Lazy.of(() -> isAnnotationPresent(Version.class));
7982
private final Lazy<TypeInformation<?>> associationTargetType = Lazy.of(() -> {
8083

@@ -329,4 +332,19 @@ private Stream<? extends AnnotatedElement> getAccessors() {
329332
return Optionals.toStream(Optional.ofNullable(getGetter()), Optional.ofNullable(getSetter()),
330333
Optional.ofNullable(getField()));
331334
}
335+
336+
/**
337+
* Load jMolecules' {@code @Identity} type if present on the classpath. Dedicated method instead of a simple static
338+
* initializer to be able to suppress the compiler warning.
339+
*
340+
* @return can be {@literal null}.
341+
*/
342+
@Nullable
343+
@SuppressWarnings("unchecked")
344+
private static Class<? extends Annotation> loadIdentityType() {
345+
346+
return (Class<? extends Annotation>) ReflectionUtils.loadIfPresent(
347+
"org.jmolecules.ddd.annotation.Identity",
348+
AbstractPersistentProperty.class.getClassLoader());
349+
}
332350
}

src/test/java/org/springframework/data/mapping/model/AnnotationBasedPersistentPropertyUnitTests.java

+10
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.stream.Stream;
2929

3030
import org.assertj.core.api.ClassAssert;
31+
import org.jmolecules.ddd.annotation.Identity;
3132
import org.jmolecules.ddd.types.AggregateRoot;
3233
import org.jmolecules.ddd.types.Association;
3334
import org.jmolecules.ddd.types.Identifier;
@@ -329,6 +330,14 @@ void exposesAssociationTargetClassAsPersistentEntityType() {
329330
.allMatch(it -> it.equals(ClassTypeInformation.from(Sample.class)));
330331
}
331332

333+
@Test // #2438
334+
void detectsJMoleculesIdentity() {
335+
336+
SamplePersistentProperty property = getProperty(JMolecules.class, "identifier");
337+
338+
assertThat(property.isIdProperty()).isTrue();
339+
}
340+
332341
@SuppressWarnings("unchecked")
333342
private Map<Class<? extends Annotation>, Annotation> getAnnotationCache(SamplePersistentProperty property) {
334343
return (Map<Class<? extends Annotation>, Annotation>) ReflectionTestUtils.getField(property, "annotationCache");
@@ -515,6 +524,7 @@ interface NoField {
515524
}
516525

517526
static class JMolecules {
527+
@Identity Long identifier;
518528
Association<JMoleculesAggregate, Identifier> association;
519529
}
520530

0 commit comments

Comments
 (0)