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 3accd0914..7fc3720b3 100644 --- a/src/main/java/org/springframework/data/mapping/context/PersistentEntities.java +++ b/src/main/java/org/springframework/data/mapping/context/PersistentEntities.java @@ -30,6 +30,7 @@ import java.util.stream.StreamSupport; import org.springframework.data.mapping.MappingException; import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.PersistentProperty; +import org.springframework.data.util.Lazy; import org.springframework.data.util.Streamable; import org.springframework.data.util.TypeInformation; import org.springframework.lang.Nullable; @@ -46,7 +47,7 @@ import org.springframework.util.Assert; */ public class PersistentEntities implements Streamable>> { - private final Collection>> contexts; + private final Lazy>>> contexts; /** * Creates a new {@link PersistentEntities} for the given {@link MappingContext}s. @@ -58,9 +59,9 @@ public class PersistentEntities implements Streamable contexts instanceof Collection ? (Collection>>) contexts - : StreamSupport.stream(contexts.spliterator(), false).collect(Collectors.toList()); + : StreamSupport.stream(contexts.spliterator(), false).toList()); } /** @@ -88,7 +89,7 @@ public class PersistentEntities implements Streamable>> getPersistentEntity(Class type) { - for (MappingContext> context : contexts) { + for (MappingContext> context : getMappingContexts()) { if (context.hasPersistentEntityFor(type)) { return Optional.of(context.getRequiredPersistentEntity(type)); } @@ -112,14 +113,16 @@ public class PersistentEntities implements Streamable>> mappingContexts = getMappingContexts(); + + if (mappingContexts.size() == 1) { + return mappingContexts.iterator().next().getRequiredPersistentEntity(type); } return getPersistentEntity(type).orElseThrow(() -> { return new MappingException(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())); + type.getName(), mappingContexts.size())); }); } @@ -138,14 +141,16 @@ public class PersistentEntities implements Streamable>> mappingContexts = getMappingContexts(); + + if (mappingContexts.size() == 1) { + return mappingContexts.stream() // .filter(it -> it.getPersistentEntity(type) != null) // .map(it -> combiner.apply(it, it.getRequiredPersistentEntity(type))) // .findFirst(); } - return contexts.stream() // + return mappingContexts.stream() // .filter(it -> it.hasPersistentEntityFor(type)) // .map(it -> combiner.apply(it, it.getRequiredPersistentEntity(type))) // .findFirst(); @@ -160,7 +165,7 @@ public class PersistentEntities implements Streamable> target = new HashSet<>(); - for (MappingContext> context : contexts) { + for (MappingContext> context : getMappingContexts()) { target.addAll(context.getManagedTypes()); } @@ -172,7 +177,7 @@ public class PersistentEntities implements Streamable>> target = new ArrayList<>(); - for (MappingContext> context : contexts) { + for (MappingContext> context : getMappingContexts()) { target.addAll(context.getPersistentEntities()); } @@ -236,7 +241,7 @@ public class PersistentEntities implements Streamable getEntityIdentifiedBy(TypeInformation type) { Collection> entities = new ArrayList<>(); - for (MappingContext> context : contexts) { + for (MappingContext> context : getMappingContexts()) { for (PersistentEntity> persistentProperties : context .getPersistentEntities()) { @@ -261,4 +266,9 @@ public class PersistentEntities implements Streamable>> getMappingContexts() { + return this.contexts.get(); + } + } diff --git a/src/test/java/org/springframework/data/mapping/context/PersistentEntitiesUnitTests.java b/src/test/java/org/springframework/data/mapping/context/PersistentEntitiesUnitTests.java index eb04002df..3a8c1c923 100755 --- a/src/test/java/org/springframework/data/mapping/context/PersistentEntitiesUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/context/PersistentEntitiesUnitTests.java @@ -35,7 +35,7 @@ import org.springframework.data.util.TypeInformation; * * @author Oliver Gierke * @author Christoph Strobl - * @author Mar Paluch + * @author Mark Paluch */ @ExtendWith(MockitoExtension.class) class PersistentEntitiesUnitTests { @@ -48,6 +48,16 @@ class PersistentEntitiesUnitTests { assertThatIllegalArgumentException().isThrownBy(() -> new PersistentEntities(null)); } + @Test // GH-3310 + @SuppressWarnings({ "unchecked", "rawtypes" }) + void lazilyAccessesIterableOfMappingContext() { + + Iterable iterable = mock(Iterable.class); + new PersistentEntities(iterable); + + verifyNoInteractions(iterable); + } + @Test // DATACMNS-458 void returnsPersistentEntitiesFromMappingContexts() {