27
27
import org .slf4j .LoggerFactory ;
28
28
import org .springframework .dao .InvalidDataAccessApiUsageException ;
29
29
import org .springframework .data .domain .Sort ;
30
+ import org .springframework .data .mapping .Association ;
31
+ import org .springframework .data .mapping .AssociationHandler ;
30
32
import org .springframework .data .mapping .PropertyHandler ;
33
+ import org .springframework .data .mapping .model .MappingException ;
31
34
import org .springframework .data .mongodb .core .index .Index .Duplicates ;
32
35
import org .springframework .data .mongodb .core .index .MongoPersistentEntityIndexResolver .TextIndexIncludeOptions .IncludeStrategy ;
33
36
import org .springframework .data .mongodb .core .index .TextIndexDefinition .TextIndexDefinitionBuilder ;
@@ -123,6 +126,8 @@ public void doWithPersistentProperty(MongoPersistentProperty persistentProperty)
123
126
}
124
127
});
125
128
129
+ indexInformation .addAll (resolveIndexesForDbrefs ("" , root .getCollection (), root ));
130
+
126
131
return indexInformation ;
127
132
}
128
133
@@ -168,6 +173,8 @@ public void doWithPersistentProperty(MongoPersistentProperty persistentProperty)
168
173
}
169
174
});
170
175
176
+ indexInformation .addAll (resolveIndexesForDbrefs (path , collection , entity ));
177
+
171
178
return indexInformation ;
172
179
}
173
180
@@ -193,18 +200,19 @@ private List<IndexDefinitionHolder> potentiallyCreateCompoundIndexDefinitions(St
193
200
return createCompoundIndexDefinitions (dotPath , collection , entity );
194
201
}
195
202
196
- private Collection <? extends IndexDefinitionHolder > potentiallyCreateTextIndexDefinition (MongoPersistentEntity <?> root ) {
203
+ private Collection <? extends IndexDefinitionHolder > potentiallyCreateTextIndexDefinition (
204
+ MongoPersistentEntity <?> root ) {
197
205
198
- TextIndexDefinitionBuilder indexDefinitionBuilder = new TextIndexDefinitionBuilder (). named ( root . getType ()
199
- .getSimpleName () + "_TextIndex" );
206
+ TextIndexDefinitionBuilder indexDefinitionBuilder = new TextIndexDefinitionBuilder ()
207
+ .named ( root . getType (). getSimpleName () + "_TextIndex" );
200
208
201
209
if (StringUtils .hasText (root .getLanguage ())) {
202
210
indexDefinitionBuilder .withDefaultLanguage (root .getLanguage ());
203
211
}
204
212
205
213
try {
206
- appendTextIndexInformation ("" , indexDefinitionBuilder , root ,
207
- new TextIndexIncludeOptions ( IncludeStrategy . DEFAULT ), new CycleGuard ());
214
+ appendTextIndexInformation ("" , indexDefinitionBuilder , root , new TextIndexIncludeOptions ( IncludeStrategy . DEFAULT ),
215
+ new CycleGuard ());
208
216
} catch (CyclicPropertyReferenceException e ) {
209
217
LOGGER .info (e .getMessage ());
210
218
}
@@ -220,9 +228,8 @@ private Collection<? extends IndexDefinitionHolder> potentiallyCreateTextIndexDe
220
228
221
229
}
222
230
223
- private void appendTextIndexInformation (final String dotPath ,
224
- final TextIndexDefinitionBuilder indexDefinitionBuilder , final MongoPersistentEntity <?> entity ,
225
- final TextIndexIncludeOptions includeOptions , final CycleGuard guard ) {
231
+ private void appendTextIndexInformation (final String dotPath , final TextIndexDefinitionBuilder indexDefinitionBuilder ,
232
+ final MongoPersistentEntity <?> entity , final TextIndexIncludeOptions includeOptions , final CycleGuard guard ) {
226
233
227
234
entity .doWithProperties (new PropertyHandler <MongoPersistentProperty >() {
228
235
@@ -249,8 +256,8 @@ public void doWithPersistentProperty(MongoPersistentProperty persistentProperty)
249
256
250
257
TextIndexIncludeOptions optionsForNestedType = includeOptions ;
251
258
if (!IncludeStrategy .FORCE .equals (includeOptions .getStrategy ()) && indexed != null ) {
252
- optionsForNestedType = new TextIndexIncludeOptions (IncludeStrategy .FORCE , new TextIndexedFieldSpec (
253
- propertyDotPath , weight ));
259
+ optionsForNestedType = new TextIndexIncludeOptions (IncludeStrategy .FORCE ,
260
+ new TextIndexedFieldSpec ( propertyDotPath , weight ));
254
261
}
255
262
256
263
try {
@@ -259,9 +266,8 @@ public void doWithPersistentProperty(MongoPersistentProperty persistentProperty)
259
266
} catch (CyclicPropertyReferenceException e ) {
260
267
LOGGER .info (e .getMessage (), e );
261
268
} catch (InvalidDataAccessApiUsageException e ) {
262
- LOGGER .info (
263
- String .format ("Potentially invalid index structure discovered. Breaking operation for %s." ,
264
- entity .getName ()), e );
269
+ LOGGER .info (String .format ("Potentially invalid index structure discovered. Breaking operation for %s." ,
270
+ entity .getName ()), e );
265
271
}
266
272
} else if (includeOptions .isForce () || indexed != null ) {
267
273
indexDefinitionBuilder .onField (propertyDotPath , weight );
@@ -306,8 +312,8 @@ protected List<IndexDefinitionHolder> createCompoundIndexDefinitions(String dotP
306
312
protected IndexDefinitionHolder createCompoundIndexDefinition (String dotPath , String fallbackCollection ,
307
313
CompoundIndex index , MongoPersistentEntity <?> entity ) {
308
314
309
- CompoundIndexDefinition indexDefinition = new CompoundIndexDefinition (resolveCompoundIndexKeyFromStringDefinition (
310
- dotPath , index .def ()));
315
+ CompoundIndexDefinition indexDefinition = new CompoundIndexDefinition (
316
+ resolveCompoundIndexKeyFromStringDefinition ( dotPath , index .def ()));
311
317
312
318
if (!index .useGeneratedName ()) {
313
319
indexDefinition .named (pathAwareIndexName (index .name (), dotPath , null ));
@@ -431,13 +437,45 @@ private String pathAwareIndexName(String indexName, String dotPath, MongoPersist
431
437
432
438
if (StringUtils .hasText (dotPath )) {
433
439
434
- nameToUse = StringUtils .hasText (nameToUse ) ? (property != null ? dotPath .replace ("." + property .getFieldName (),
435
- "" ) : dotPath ) + "." + nameToUse : dotPath ;
440
+ nameToUse = StringUtils .hasText (nameToUse )
441
+ ? (property != null ? dotPath .replace ("." + property .getFieldName (), "" ) : dotPath ) + "." + nameToUse
442
+ : dotPath ;
436
443
}
437
444
return nameToUse ;
438
445
439
446
}
440
447
448
+ private List <IndexDefinitionHolder > resolveIndexesForDbrefs (final String path , final String collection ,
449
+ MongoPersistentEntity <?> entity ) {
450
+
451
+ final List <IndexDefinitionHolder > indexes = new ArrayList <IndexDefinitionHolder >(0 );
452
+ entity .doWithAssociations (new AssociationHandler <MongoPersistentProperty >() {
453
+
454
+ @ Override
455
+ public void doWithAssociation (Association <MongoPersistentProperty > association ) {
456
+
457
+ MongoPersistentProperty property = association .getInverse ();
458
+
459
+ String propertyDotPath = (StringUtils .hasText (path ) ? path + "." : "" ) + property .getFieldName ();
460
+
461
+ if (property .isAnnotationPresent (GeoSpatialIndexed .class ) || property .isAnnotationPresent (TextIndexed .class )) {
462
+ throw new MappingException (
463
+ String .format ("Cannot create geospatial-/text- index on DBRef in collection '%s' for path '%s'." ,
464
+ collection , propertyDotPath ));
465
+ }
466
+
467
+ IndexDefinitionHolder indexDefinitionHolder = createIndexDefinitionHolderForProperty (propertyDotPath ,
468
+ collection , property );
469
+
470
+ if (indexDefinitionHolder != null ) {
471
+ indexes .add (indexDefinitionHolder );
472
+ }
473
+ }
474
+ });
475
+
476
+ return indexes ;
477
+ }
478
+
441
479
/**
442
480
* {@link CycleGuard} holds information about properties and the paths for accessing those. This information is used
443
481
* to detect potential cycles within the references.
0 commit comments