|
25 | 25 |
|
26 | 26 | import org.slf4j.Logger;
|
27 | 27 | import org.slf4j.LoggerFactory;
|
28 |
| -import org.springframework.core.annotation.AnnotationUtils; |
29 | 28 | import org.springframework.dao.InvalidDataAccessApiUsageException;
|
30 | 29 | import org.springframework.data.domain.Sort;
|
31 | 30 | import org.springframework.data.mapping.PropertyHandler;
|
@@ -96,7 +95,7 @@ public List<IndexDefinitionHolder> resolveIndexForEntity(final MongoPersistentEn
|
96 | 95 | Assert.notNull(document, "Given entity is not collection root.");
|
97 | 96 |
|
98 | 97 | final List<IndexDefinitionHolder> indexInformation = new ArrayList<MongoPersistentEntityIndexResolver.IndexDefinitionHolder>();
|
99 |
| - indexInformation.addAll(potentiallyCreateCompoundIndexDefinitions("", root.getCollection(), root.getType())); |
| 98 | + indexInformation.addAll(potentiallyCreateCompoundIndexDefinitions("", root.getCollection(), root)); |
100 | 99 | indexInformation.addAll(potentiallyCreateTextIndexDefinition(root));
|
101 | 100 |
|
102 | 101 | final CycleGuard guard = new CycleGuard();
|
@@ -138,10 +137,11 @@ public void doWithPersistentProperty(MongoPersistentProperty persistentProperty)
|
138 | 137 | private List<IndexDefinitionHolder> resolveIndexForClass(final Class<?> type, final String path,
|
139 | 138 | final String collection, final CycleGuard guard) {
|
140 | 139 |
|
| 140 | + MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(type); |
| 141 | + |
141 | 142 | final List<IndexDefinitionHolder> indexInformation = new ArrayList<MongoPersistentEntityIndexResolver.IndexDefinitionHolder>();
|
142 |
| - indexInformation.addAll(potentiallyCreateCompoundIndexDefinitions(path, collection, type)); |
| 143 | + indexInformation.addAll(potentiallyCreateCompoundIndexDefinitions(path, collection, entity)); |
143 | 144 |
|
144 |
| - MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(type); |
145 | 145 | entity.doWithProperties(new PropertyHandler<MongoPersistentProperty>() {
|
146 | 146 |
|
147 | 147 | @Override
|
@@ -183,14 +183,13 @@ private IndexDefinitionHolder createIndexDefinitionHolderForProperty(String dotP
|
183 | 183 | }
|
184 | 184 |
|
185 | 185 | private List<IndexDefinitionHolder> potentiallyCreateCompoundIndexDefinitions(String dotPath, String collection,
|
186 |
| - Class<?> type) { |
| 186 | + MongoPersistentEntity<?> entity) { |
187 | 187 |
|
188 |
| - if (AnnotationUtils.findAnnotation(type, CompoundIndexes.class) == null |
189 |
| - && AnnotationUtils.findAnnotation(type, CompoundIndex.class) == null) { |
| 188 | + if (entity.findAnnotation(CompoundIndexes.class) == null && entity.findAnnotation(CompoundIndex.class) == null) { |
190 | 189 | return Collections.emptyList();
|
191 | 190 | }
|
192 | 191 |
|
193 |
| - return createCompoundIndexDefinitions(dotPath, collection, type); |
| 192 | + return createCompoundIndexDefinitions(dotPath, collection, entity); |
194 | 193 | }
|
195 | 194 |
|
196 | 195 | private Collection<? extends IndexDefinitionHolder> potentiallyCreateTextIndexDefinition(MongoPersistentEntity<?> root) {
|
@@ -278,35 +277,35 @@ public void doWithPersistentProperty(MongoPersistentProperty persistentProperty)
|
278 | 277 | * @return
|
279 | 278 | */
|
280 | 279 | protected List<IndexDefinitionHolder> createCompoundIndexDefinitions(String dotPath, String fallbackCollection,
|
281 |
| - Class<?> type) { |
| 280 | + MongoPersistentEntity<?> entity) { |
282 | 281 |
|
283 | 282 | List<IndexDefinitionHolder> indexDefinitions = new ArrayList<MongoPersistentEntityIndexResolver.IndexDefinitionHolder>();
|
284 |
| - CompoundIndexes indexes = AnnotationUtils.findAnnotation(type, CompoundIndexes.class); |
| 283 | + CompoundIndexes indexes = entity.findAnnotation(CompoundIndexes.class); |
285 | 284 |
|
286 | 285 | if (indexes != null) {
|
287 | 286 | for (CompoundIndex index : indexes.value()) {
|
288 |
| - indexDefinitions.add(createCompoundIndexDefinition(dotPath, fallbackCollection, index)); |
| 287 | + indexDefinitions.add(createCompoundIndexDefinition(dotPath, fallbackCollection, index, entity)); |
289 | 288 | }
|
290 | 289 | }
|
291 | 290 |
|
292 |
| - CompoundIndex index = AnnotationUtils.findAnnotation(type, CompoundIndex.class); |
| 291 | + CompoundIndex index = entity.findAnnotation(CompoundIndex.class); |
293 | 292 |
|
294 | 293 | if (index != null) {
|
295 |
| - indexDefinitions.add(createCompoundIndexDefinition(dotPath, fallbackCollection, index)); |
| 294 | + indexDefinitions.add(createCompoundIndexDefinition(dotPath, fallbackCollection, index, entity)); |
296 | 295 | }
|
297 | 296 |
|
298 | 297 | return indexDefinitions;
|
299 | 298 | }
|
300 | 299 |
|
301 | 300 | @SuppressWarnings("deprecation")
|
302 | 301 | protected IndexDefinitionHolder createCompoundIndexDefinition(String dotPath, String fallbackCollection,
|
303 |
| - CompoundIndex index) { |
| 302 | + CompoundIndex index, MongoPersistentEntity<?> entity) { |
304 | 303 |
|
305 | 304 | CompoundIndexDefinition indexDefinition = new CompoundIndexDefinition(resolveCompoundIndexKeyFromStringDefinition(
|
306 | 305 | dotPath, index.def()));
|
307 | 306 |
|
308 | 307 | if (!index.useGeneratedName()) {
|
309 |
| - indexDefinition.named(index.name()); |
| 308 | + indexDefinition.named(pathAwareIndexName(index.name(), dotPath, null)); |
310 | 309 | }
|
311 | 310 |
|
312 | 311 | if (index.unique()) {
|
@@ -377,7 +376,7 @@ protected IndexDefinitionHolder createIndexDefinition(String dotPath, String fal
|
377 | 376 | IndexDirection.ASCENDING.equals(index.direction()) ? Sort.Direction.ASC : Sort.Direction.DESC);
|
378 | 377 |
|
379 | 378 | if (!index.useGeneratedName()) {
|
380 |
| - indexDefinition.named(StringUtils.hasText(index.name()) ? index.name() : dotPath); |
| 379 | + indexDefinition.named(pathAwareIndexName(index.name(), dotPath, persitentProperty)); |
381 | 380 | }
|
382 | 381 |
|
383 | 382 | if (index.unique()) {
|
@@ -419,14 +418,31 @@ protected IndexDefinitionHolder createGeoSpatialIndexDefinition(String dotPath,
|
419 | 418 | indexDefinition.withMin(index.min()).withMax(index.max());
|
420 | 419 |
|
421 | 420 | if (!index.useGeneratedName()) {
|
422 |
| - indexDefinition.named(StringUtils.hasText(index.name()) ? index.name() : persistentProperty.getName()); |
| 421 | + indexDefinition.named(pathAwareIndexName(index.name(), dotPath, persistentProperty)); |
423 | 422 | }
|
424 | 423 |
|
425 | 424 | indexDefinition.typed(index.type()).withBucketSize(index.bucketSize()).withAdditionalField(index.additionalField());
|
426 | 425 |
|
427 | 426 | return new IndexDefinitionHolder(dotPath, indexDefinition, collection);
|
428 | 427 | }
|
429 | 428 |
|
| 429 | + private String pathAwareIndexName(String indexName, String dotPath, MongoPersistentProperty property) { |
| 430 | + |
| 431 | + String nameToUse = StringUtils.hasText(indexName) ? indexName : ""; |
| 432 | + |
| 433 | + if (!StringUtils.hasText(dotPath) || (property != null && dotPath.equals(property.getFieldName()))) { |
| 434 | + return StringUtils.hasText(nameToUse) ? nameToUse : dotPath; |
| 435 | + } |
| 436 | + |
| 437 | + if (StringUtils.hasText(dotPath)) { |
| 438 | + |
| 439 | + nameToUse = StringUtils.hasText(nameToUse) ? (property != null ? dotPath.replace("." + property.getFieldName(), |
| 440 | + "") : dotPath) + "." + nameToUse : dotPath; |
| 441 | + } |
| 442 | + return nameToUse; |
| 443 | + |
| 444 | + } |
| 445 | + |
430 | 446 | /**
|
431 | 447 | * {@link CycleGuard} holds information about properties and the paths for accessing those. This information is used
|
432 | 448 | * to detect potential cycles within the references.
|
|
0 commit comments