diff --git a/src/main/java/org/springframework/data/mapping/context/PersistentEntities.java b/src/main/java/org/springframework/data/mapping/context/PersistentEntities.java index 9ad53e850..15ce45c6a 100644 --- a/src/main/java/org/springframework/data/mapping/context/PersistentEntities.java +++ b/src/main/java/org/springframework/data/mapping/context/PersistentEntities.java @@ -39,6 +39,7 @@ import org.springframework.util.Assert; * * @author Oliver Gierke * @author Christoph Strobl + * @author Mark Paluch * @since 1.8 */ public class PersistentEntities implements Streamable>> { @@ -50,6 +51,7 @@ public class PersistentEntities implements Streamable> contexts) { Assert.notNull(contexts, "MappingContexts must not be null!"); @@ -61,7 +63,7 @@ public class PersistentEntities implements Streamable { + if (contexts.size() == 1) { + return contexts.iterator().next().getRequiredPersistentEntity(type); + } - if (contexts.size() != 1) { - - throw new IllegalArgumentException(String.format( - "Couldn't create PersistentEntity for type %s! PersistentEntities knows about %s MappingContext instances and therefore cannot tell which is the responsible one. Please set the base package in your configuration to pre initialize contexts.", - type, contexts.size())); - } - - PersistentEntity> entity = contexts.iterator().next() - .getPersistentEntity(type); - if (entity == null) { - throw new IllegalArgumentException(String.format("Couldn't find PersistentEntity for type %s!", type)); - } - return entity; + return getPersistentEntity(type).orElseThrow(() -> { + return new IllegalArgumentException(String.format( + "Cannot get or create PersistentEntity for type %s! PersistentEntities knows about %s MappingContext instances and therefore cannot identify a single responsible one. Please configure the initialEntitySet through an entity scan using the base package in your configuration to pre initialize contexts.", + type.getName(), contexts.size())); }); } /** * Executes the given {@link BiFunction} on the given {@link MappingContext} and {@link PersistentEntity} based on the - * given type. - * + * given type. Considers all {@link MappingContext}s for lookup. This method will create a new + * {@link PersistentEntity} in case there is only a single {@link MappingContext} registered. + * * @param type must not be {@literal null}. * @param combiner must not be {@literal null}. - * @return + * @return result of the {@link BiFunction}. */ public Optional mapOnContext(Class type, BiFunction>, PersistentEntity, T> combiner) { @@ -138,6 +134,13 @@ public class PersistentEntities implements Streamable it.getPersistentEntity(type) != null) // + .map(it -> combiner.apply(it, it.getRequiredPersistentEntity(type))) // + .findFirst(); + } + return contexts.stream() // .filter(it -> it.hasPersistentEntityFor(type)) // .map(it -> combiner.apply(it, it.getRequiredPersistentEntity(type))) // @@ -152,9 +155,11 @@ public class PersistentEntities implements Streamable> getManagedTypes() { Set> target = new HashSet<>(); + for (MappingContext> context : contexts) { target.addAll(context.getManagedTypes()); } + return Streamable.of(target); } @@ -166,9 +171,11 @@ public class PersistentEntities implements Streamable>> iterator() { List>> target = new ArrayList<>(); + for (MappingContext> context : contexts) { target.addAll(context.getPersistentEntities()); } + return target.iterator(); } @@ -178,7 +185,7 @@ public class PersistentEntities implements Streamable it.getType().getName()).collect(Collectors.joining(", ")); - message += "! Introduce dedciated unique identifier types or explicitly define the target type in @Reference!"; + message += "! Introduce dedicated unique identifier types or explicitly define the target type in @Reference!"; throw new IllegalStateException(message); }