@@ -240,7 +240,24 @@ public TypeInformation<?> getTypeUltimatelyReferredToBy(PersistentProperty<?> pr
240240 @ Nullable
241241 private PersistentEntity <?, ?> getEntityIdentifiedBy (TypeInformation <?> type ) {
242242
243+ Collection <PersistentEntity <?, ?>> entities = getPersistentEntities (type );
244+ 245+ if (entities .size () > 1 ) {
246+ 247+ String message = "Found multiple entities identified by " + type .getType () + ": " ;
248+ message += entities .stream ().map (it -> it .getType ().getName ()).collect (Collectors .joining (", " ));
249+ message += "; Introduce dedicated unique identifier types or explicitly define the target type in @Reference" ;
250+ 251+ throw new IllegalStateException (message );
252+ }
253+ 254+ return entities .isEmpty () ? null : entities .iterator ().next ();
255+ }
256+ 257+ private Collection <PersistentEntity <?, ?>> getPersistentEntities (TypeInformation <?> type ) {
258+ 243259 Collection <PersistentEntity <?, ?>> entities = new ArrayList <>();
260+ 244261 for (MappingContext <?, ? extends PersistentProperty <?>> context : getMappingContexts ()) {
245262
246263 for (PersistentEntity <?, ? extends PersistentProperty <?>> persistentProperties : context
@@ -255,16 +272,7 @@ public TypeInformation<?> getTypeUltimatelyReferredToBy(PersistentProperty<?> pr
255272 }
256273 }
257274
258- if (entities .size () > 1 ) {
259- 260- String message = "Found multiple entities identified by " + type .getType () + ": " ;
261- message += entities .stream ().map (it -> it .getType ().getName ()).collect (Collectors .joining (", " ));
262- message += "; Introduce dedicated unique identifier types or explicitly define the target type in @Reference" ;
263- 264- throw new IllegalStateException (message );
265- }
266- 267- return entities .isEmpty () ? null : entities .iterator ().next ();
275+ return entities ;
268276 }
269277
270278 private Collection <? extends MappingContext <?, ? extends PersistentProperty <?>>> getMappingContexts () {
0 commit comments