3232import org .springframework .data .mapping .MappingException ;
3333import org .springframework .data .mapping .PersistentEntity ;
3434import org .springframework .data .mapping .PersistentProperty ;
35+ import org .springframework .data .util .Lazy ;
3536import org .springframework .data .util .Streamable ;
3637import org .springframework .data .util .TypeInformation ;
3738import org .springframework .util .Assert ;
4748 */
4849public class PersistentEntities implements Streamable <PersistentEntity <?, ? extends PersistentProperty <?>>> {
4950
50- private final Collection <? extends MappingContext <?, ? extends PersistentProperty <?>>> contexts ;
51+ private final Lazy < Collection <? extends MappingContext <?, ? extends PersistentProperty <?> >>> contexts ;
5152
5253 /**
5354 * Creates a new {@link PersistentEntities} for the given {@link MappingContext}s.
@@ -58,9 +59,9 @@ public PersistentEntities(Iterable<? extends MappingContext<?, ?>> contexts) {
5859
5960 Assert .notNull (contexts , "MappingContexts must not be null" );
6061
61- this .contexts = contexts instanceof Collection
62+ this .contexts = Lazy . of (() -> contexts instanceof Collection
6263 ? (Collection <? extends MappingContext <?, ? extends PersistentProperty <?>>>) contexts
63- : StreamSupport .stream (contexts .spliterator (), false ).collect ( Collectors . toList ());
64+ : StreamSupport .stream (contexts .spliterator (), false ).toList ());
6465 }
6566
6667 /**
@@ -88,7 +89,7 @@ public static PersistentEntities of(MappingContext<?, ?>... contexts) {
8889 */
8990 public Optional <PersistentEntity <?, ? extends PersistentProperty <?>>> getPersistentEntity (Class <?> type ) {
9091
91- for (MappingContext <?, ? extends PersistentProperty <?>> context : contexts ) {
92+ for (MappingContext <?, ? extends PersistentProperty <?>> context : getMappingContexts () ) {
9293 if (context .hasPersistentEntityFor (type )) {
9394 return Optional .of (context .getRequiredPersistentEntity (type ));
9495 }
@@ -112,14 +113,16 @@ public static PersistentEntities of(MappingContext<?, ?>... contexts) {
112113
113114 Assert .notNull (type , "Domain type must not be null" );
114115
115- if (contexts .size () == 1 ) {
116- return contexts .iterator ().next ().getRequiredPersistentEntity (type );
116+ Collection <? extends MappingContext <?, ? extends PersistentProperty <?>>> mappingContexts = getMappingContexts ();
117+
118+ if (mappingContexts .size () == 1 ) {
119+ return mappingContexts .iterator ().next ().getRequiredPersistentEntity (type );
117120 }
118121
119122 return getPersistentEntity (type ).orElseThrow (() -> {
120123 return new MappingException (String .format (
121124 "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" ,
122- type .getName (), contexts .size ()));
125+ type .getName (), mappingContexts .size ()));
123126 });
124127 }
125128
@@ -138,14 +141,16 @@ public <T> Optional<T> mapOnContext(Class<?> type,
138141 Assert .notNull (type , "Type must not be null" );
139142 Assert .notNull (combiner , "Combining BiFunction must not be null" );
140143
141- if (contexts .size () == 1 ) {
142- return contexts .stream () //
144+ Collection <? extends MappingContext <?, ? extends PersistentProperty <?>>> mappingContexts = getMappingContexts ();
145+
146+ if (mappingContexts .size () == 1 ) {
147+ return mappingContexts .stream () //
143148 .filter (it -> it .getPersistentEntity (type ) != null ) //
144149 .map (it -> combiner .apply (it , it .getRequiredPersistentEntity (type ))) //
145150 .findFirst ();
146151 }
147152
148- return contexts .stream () //
153+ return mappingContexts .stream () //
149154 .filter (it -> it .hasPersistentEntityFor (type )) //
150155 .map (it -> combiner .apply (it , it .getRequiredPersistentEntity (type ))) //
151156 .findFirst ();
@@ -160,7 +165,7 @@ public Streamable<TypeInformation<?>> getManagedTypes() {
160165
161166 Set <TypeInformation <?>> target = new HashSet <>();
162167
163- for (MappingContext <?, ? extends PersistentProperty <?>> context : contexts ) {
168+ for (MappingContext <?, ? extends PersistentProperty <?>> context : getMappingContexts () ) {
164169 target .addAll (context .getManagedTypes ());
165170 }
166171
@@ -172,7 +177,7 @@ public Streamable<TypeInformation<?>> getManagedTypes() {
172177
173178 List <PersistentEntity <?, ? extends PersistentProperty <?>>> target = new ArrayList <>();
174179
175- for (MappingContext <?, ? extends PersistentProperty <?>> context : contexts ) {
180+ for (MappingContext <?, ? extends PersistentProperty <?>> context : getMappingContexts () ) {
176181 target .addAll (context .getPersistentEntities ());
177182 }
178183
@@ -234,7 +239,7 @@ public TypeInformation<?> getTypeUltimatelyReferredToBy(PersistentProperty<?> pr
234239 private @ Nullable PersistentEntity <?, ?> getEntityIdentifiedBy (TypeInformation <?> type ) {
235240
236241 Collection <PersistentEntity <?, ?>> entities = new ArrayList <>();
237- for (MappingContext <?, ? extends PersistentProperty <?>> context : contexts ) {
242+ for (MappingContext <?, ? extends PersistentProperty <?>> context : getMappingContexts () ) {
238243
239244 for (PersistentEntity <?, ? extends PersistentProperty <?>> persistentProperties : context
240245 .getPersistentEntities ()) {
@@ -259,4 +264,9 @@ public TypeInformation<?> getTypeUltimatelyReferredToBy(PersistentProperty<?> pr
259264
260265 return entities .isEmpty () ? null : entities .iterator ().next ();
261266 }
267+
268+ private Collection <? extends MappingContext <?, ? extends PersistentProperty <?>>> getMappingContexts () {
269+ return this .contexts .get ();
270+ }
271+
262272}
0 commit comments