@@ -238,7 +238,24 @@ public TypeInformation<?> getTypeUltimatelyReferredToBy(PersistentProperty<?> pr
238238 */
239239 private @ Nullable PersistentEntity <?, ?> getEntityIdentifiedBy (TypeInformation <?> type ) {
240240
241+ Collection <PersistentEntity <?, ?>> entities = getPersistentEntities (type );
242+
243+ if (entities .size () > 1 ) {
244+
245+ String message = "Found multiple entities identified by " + type .getType () + ": " ;
246+ message += entities .stream ().map (it -> it .getType ().getName ()).collect (Collectors .joining (", " ));
247+ message += "; Introduce dedicated unique identifier types or explicitly define the target type in @Reference" ;
248+
249+ throw new IllegalStateException (message );
250+ }
251+
252+ return entities .isEmpty () ? null : entities .iterator ().next ();
253+ }
254+
255+ private Collection <PersistentEntity <?, ?>> getPersistentEntities (TypeInformation <?> type ) {
256+
241257 Collection <PersistentEntity <?, ?>> entities = new ArrayList <>();
258+
242259 for (MappingContext <?, ? extends PersistentProperty <?>> context : getMappingContexts ()) {
243260
244261 for (PersistentEntity <?, ? extends PersistentProperty <?>> persistentProperties : context
@@ -253,16 +270,7 @@ public TypeInformation<?> getTypeUltimatelyReferredToBy(PersistentProperty<?> pr
253270 }
254271 }
255272
256- if (entities .size () > 1 ) {
257-
258- String message = "Found multiple entities identified by " + type .getType () + ": " ;
259- message += entities .stream ().map (it -> it .getType ().getName ()).collect (Collectors .joining (", " ));
260- message += "; Introduce dedicated unique identifier types or explicitly define the target type in @Reference" ;
261-
262- throw new IllegalStateException (message );
263- }
264-
265- return entities .isEmpty () ? null : entities .iterator ().next ();
273+ return entities ;
266274 }
267275
268276 private Collection <? extends MappingContext <?, ? extends PersistentProperty <?>>> getMappingContexts () {
0 commit comments