40
40
import org .springframework .data .util .ClassTypeInformation ;
41
41
import org .springframework .data .util .Lazy ;
42
42
import org .springframework .data .util .Optionals ;
43
+ import org .springframework .data .util .ReflectionUtils ;
43
44
import org .springframework .data .util .StreamUtils ;
44
45
import org .springframework .data .util .TypeInformation ;
45
46
import org .springframework .lang .Nullable ;
@@ -56,6 +57,7 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp
56
57
extends AbstractPersistentProperty <P > {
57
58
58
59
private static final String SPRING_DATA_PACKAGE = "org.springframework.data" ;
60
+ private static final Class <? extends Annotation > IDENTITY_TYPE = loadIdentityType ();
59
61
60
62
private final @ Nullable String value ;
61
63
private final Map <Class <? extends Annotation >, Optional <? extends Annotation >> annotationCache = new ConcurrentHashMap <>();
@@ -74,7 +76,8 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp
74
76
.of (() -> !isTransient () && !isAnnotationPresent (ReadOnlyProperty .class ));
75
77
private final Lazy <Boolean > isReference = Lazy .of (() -> !isTransient () //
76
78
&& (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 ));
78
81
private final Lazy <Boolean > isVersion = Lazy .of (() -> isAnnotationPresent (Version .class ));
79
82
private final Lazy <TypeInformation <?>> associationTargetType = Lazy .of (() -> {
80
83
@@ -329,4 +332,19 @@ private Stream<? extends AnnotatedElement> getAccessors() {
329
332
return Optionals .toStream (Optional .ofNullable (getGetter ()), Optional .ofNullable (getSetter ()),
330
333
Optional .ofNullable (getField ()));
331
334
}
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
+ }
332
350
}
0 commit comments