30
30
import org .springframework .data .mapping .MappingException ;
31
31
import org .springframework .data .mapping .PersistentEntity ;
32
32
import org .springframework .data .mapping .PersistentProperty ;
33
+ import org .springframework .data .util .Lazy ;
33
34
import org .springframework .data .util .Streamable ;
34
35
import org .springframework .data .util .TypeInformation ;
35
36
import org .springframework .lang .Nullable ;
46
47
*/
47
48
public class PersistentEntities implements Streamable <PersistentEntity <?, ? extends PersistentProperty <?>>> {
48
49
49
- private final Collection <? extends MappingContext <?, ? extends PersistentProperty <?>>> contexts ;
50
+ private final Lazy < Collection <? extends MappingContext <?, ? extends PersistentProperty <?> >>> contexts ;
50
51
51
52
/**
52
53
* Creates a new {@link PersistentEntities} for the given {@link MappingContext}s.
@@ -58,9 +59,9 @@ public PersistentEntities(Iterable<? extends MappingContext<?, ?>> contexts) {
58
59
59
60
Assert .notNull (contexts , "MappingContexts must not be null" );
60
61
61
- this .contexts = contexts instanceof Collection
62
+ this .contexts = Lazy . of (() -> contexts instanceof Collection
62
63
? (Collection <? extends MappingContext <?, ? extends PersistentProperty <?>>>) contexts
63
- : StreamSupport .stream (contexts .spliterator (), false ).collect ( Collectors . toList ());
64
+ : StreamSupport .stream (contexts .spliterator (), false ).toList ());
64
65
}
65
66
66
67
/**
@@ -88,7 +89,7 @@ public static PersistentEntities of(MappingContext<?, ?>... contexts) {
88
89
*/
89
90
public Optional <PersistentEntity <?, ? extends PersistentProperty <?>>> getPersistentEntity (Class <?> type ) {
90
91
91
- for (MappingContext <?, ? extends PersistentProperty <?>> context : contexts ) {
92
+ for (MappingContext <?, ? extends PersistentProperty <?>> context : getMappingContexts () ) {
92
93
if (context .hasPersistentEntityFor (type )) {
93
94
return Optional .of (context .getRequiredPersistentEntity (type ));
94
95
}
@@ -112,14 +113,16 @@ public static PersistentEntities of(MappingContext<?, ?>... contexts) {
112
113
113
114
Assert .notNull (type , "Domain type must not be null" );
114
115
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 );
117
120
}
118
121
119
122
return getPersistentEntity (type ).orElseThrow (() -> {
120
123
return new MappingException (String .format (
121
124
"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 ()));
123
126
});
124
127
}
125
128
@@ -138,14 +141,16 @@ public <T> Optional<T> mapOnContext(Class<?> type,
138
141
Assert .notNull (type , "Type must not be null" );
139
142
Assert .notNull (combiner , "Combining BiFunction must not be null" );
140
143
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 () //
143
148
.filter (it -> it .getPersistentEntity (type ) != null ) //
144
149
.map (it -> combiner .apply (it , it .getRequiredPersistentEntity (type ))) //
145
150
.findFirst ();
146
151
}
147
152
148
- return contexts .stream () //
153
+ return mappingContexts .stream () //
149
154
.filter (it -> it .hasPersistentEntityFor (type )) //
150
155
.map (it -> combiner .apply (it , it .getRequiredPersistentEntity (type ))) //
151
156
.findFirst ();
@@ -160,7 +165,7 @@ public Streamable<TypeInformation<?>> getManagedTypes() {
160
165
161
166
Set <TypeInformation <?>> target = new HashSet <>();
162
167
163
- for (MappingContext <?, ? extends PersistentProperty <?>> context : contexts ) {
168
+ for (MappingContext <?, ? extends PersistentProperty <?>> context : getMappingContexts () ) {
164
169
target .addAll (context .getManagedTypes ());
165
170
}
166
171
@@ -172,7 +177,7 @@ public Streamable<TypeInformation<?>> getManagedTypes() {
172
177
173
178
List <PersistentEntity <?, ? extends PersistentProperty <?>>> target = new ArrayList <>();
174
179
175
- for (MappingContext <?, ? extends PersistentProperty <?>> context : contexts ) {
180
+ for (MappingContext <?, ? extends PersistentProperty <?>> context : getMappingContexts () ) {
176
181
target .addAll (context .getPersistentEntities ());
177
182
}
178
183
@@ -236,7 +241,7 @@ public TypeInformation<?> getTypeUltimatelyReferredToBy(PersistentProperty<?> pr
236
241
private PersistentEntity <?, ?> getEntityIdentifiedBy (TypeInformation <?> type ) {
237
242
238
243
Collection <PersistentEntity <?, ?>> entities = new ArrayList <>();
239
- for (MappingContext <?, ? extends PersistentProperty <?>> context : contexts ) {
244
+ for (MappingContext <?, ? extends PersistentProperty <?>> context : getMappingContexts () ) {
240
245
241
246
for (PersistentEntity <?, ? extends PersistentProperty <?>> persistentProperties : context
242
247
.getPersistentEntities ()) {
@@ -261,4 +266,9 @@ public TypeInformation<?> getTypeUltimatelyReferredToBy(PersistentProperty<?> pr
261
266
262
267
return entities .isEmpty () ? null : entities .iterator ().next ();
263
268
}
269
+
270
+ private Collection <? extends MappingContext <?, ? extends PersistentProperty <?>>> getMappingContexts () {
271
+ return this .contexts .get ();
272
+ }
273
+
264
274
}
0 commit comments