4040import org .springframework .data .util .ClassTypeInformation ;
4141import org .springframework .data .util .Lazy ;
4242import org .springframework .data .util .Optionals ;
43+ import org .springframework .data .util .ReflectionUtils ;
4344import org .springframework .data .util .StreamUtils ;
4445import org .springframework .data .util .TypeInformation ;
4546import 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}
0 commit comments