Skip to content

Commit 33c48d0

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-1854 - Remove collation annotation in favor of attributes of Query / Document.
1 parent 2d95478 commit 33c48d0

File tree

13 files changed

+77
-135
lines changed

13 files changed

+77
-135
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentEntity.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,13 @@ public BasicMongoPersistentEntity(TypeInformation<T> typeInformation) {
8282
this.collection = StringUtils.hasText(document.collection()) ? document.collection() : fallback;
8383
this.language = StringUtils.hasText(document.language()) ? document.language() : "";
8484
this.expression = detectExpression(document.collection());
85+
this.collation = document.collation();
86+
this.collationExpression = detectExpression(document.collation());
8587
} else {
8688

8789
this.collection = fallback;
8890
this.language = "";
8991
this.expression = null;
90-
}
91-
92-
if (this.isAnnotationPresent(Collation.class)) {
93-
94-
Collation collation = getRequiredAnnotation(Collation.class);
95-
96-
this.collation = collation.value();
97-
this.collationExpression = detectExpression(collation.value());
98-
} else {
99-
10092
this.collation = null;
10193
this.collationExpression = null;
10294
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/Collation.java

Lines changed: 0 additions & 82 deletions
This file was deleted.

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/Document.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,17 @@
6060
/**
6161
* Defines the default language to be used with this document.
6262
*
63-
* @since 1.6
6463
* @return
64+
* @since 1.6
6565
*/
6666
String language() default "";
6767

68+
/**
69+
* Defines the collation to apply when executing a query or creating indexes.
70+
*
71+
* @return an empty {@link String} by default.
72+
* @since 2.2
73+
*/
74+
String collation() default "";
75+
6876
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/Query.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,34 @@
9595
* @since 2.1
9696
*/
9797
String sort() default "";
98+
99+
/**
100+
* Defines the collation to apply when executing the query.
101+
*
102+
* <pre class="code">
103+
* // Fixed value
104+
* &#64;Query(collation = "en_US")
105+
* List<Entry> findAllByFixedCollation();
106+
*
107+
* // Fixed value as Document
108+
* &#64;Query(collation = "{ 'locale' : 'en_US' }")
109+
* List<Entry> findAllByFixedJsonCollation();
110+
*
111+
* // Dynamic value as String
112+
* &#64;Query(collation = "?0")
113+
* List<Entry> findAllByDynamicCollation(String collation);
114+
*
115+
* // Dynamic value as Document
116+
* &#64;Query(collation = "{ 'locale' : ?0 }")
117+
* List<Entry> findAllByDynamicJsonCollation(String collation);
118+
*
119+
* // SpEL expression
120+
* &#64;Query(collation = "?#{[0]}")
121+
* List<Entry> findAllByDynamicSpElCollation(String collation);
122+
* </pre>
123+
*
124+
* @return an empty {@link String} by default.
125+
* @since 2.2
126+
*/
127+
String collation() default "";
98128
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoQueryMethod.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.springframework.data.geo.GeoResult;
2929
import org.springframework.data.geo.GeoResults;
3030
import org.springframework.data.mapping.context.MappingContext;
31-
import org.springframework.data.mongodb.core.mapping.Collation;
3231
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
3332
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
3433
import org.springframework.data.mongodb.repository.Meta;
@@ -324,34 +323,29 @@ public String getAnnotatedSort() {
324323
}
325324

326325
/**
327-
* Check if the query method is decorated with an non empty {@link Collation#collation()}.
326+
* Check if the query method is decorated with an non empty {@link Query#collation()}.
328327
*
329-
* @return true if method annotated with {@link Collation} having an non empty collation attribute.
328+
* @return true if method annotated with {@link Query} having an non empty collation attribute.
330329
* @since 2.2
331330
*/
332331
public boolean hasAnnotatedCollation() {
333-
return lookupCollationAnnotation().map(it -> !it.collation().isEmpty()).orElse(false);
332+
return lookupQueryAnnotation().map(it -> !it.collation().isEmpty()).orElse(false);
334333
}
335334

336335
/**
337-
* Get the collation value extracted from the {@link Collation} annotation.
336+
* Get the collation value extracted from the {@link Query} annotation.
338337
*
339-
* @return the {@link Collation#collation()} value.
340-
* @throws IllegalStateException if method not annotated with {@link Collation}. Make sure to check
338+
* @return the {@link Query#collation()} value.
339+
* @throws IllegalStateException if method not annotated with {@link Query}. Make sure to check
341340
* {@link #hasAnnotatedQuery()} first.
342341
* @since 2.2
343342
*/
344343
public String getAnnotatedCollation() {
345344

346-
return lookupCollationAnnotation().map(Collation::collation).orElseThrow(() -> new IllegalStateException(
345+
return lookupQueryAnnotation().map(Query::collation).orElseThrow(() -> new IllegalStateException(
347346
"Expected to find @Query annotation but did not. Make sure to check hasAnnotatedCollation() before."));
348347
}
349348

350-
Optional<Collation> lookupCollationAnnotation() {
351-
return doFindAnnotation(Collation.class);
352-
}
353-
354-
355349
@SuppressWarnings("unchecked")
356350
private <A extends Annotation> Optional<A> doFindAnnotation(Class<A> annotationType) {
357351

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultIndexOperationsUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ static class Jedi {
113113
@Field("firstname") String name;
114114
}
115115

116-
@org.springframework.data.mongodb.core.mapping.Collation("de_AT")
116+
@org.springframework.data.mongodb.core.mapping.Document(collation = "de_AT")
117117
static class Sith {
118118
@Field("firstname") String name;
119119
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultReactiveIndexOperationsUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ static class Jedi {
118118
@Field("firstname") String name;
119119
}
120120

121-
@org.springframework.data.mongodb.core.mapping.Collation("de_AT")
121+
@org.springframework.data.mongodb.core.mapping.Document(collation = "de_AT")
122122
static class Sith {
123123
@Field("firstname") String name;
124124
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1467,7 +1467,7 @@ static class WithNamedFields {
14671467
@Field("custom-named-field") String customName;
14681468
}
14691469

1470-
@org.springframework.data.mongodb.core.mapping.Collation("de_AT")
1470+
@org.springframework.data.mongodb.core.mapping.Document(collation = "de_AT")
14711471
static class Sith {
14721472

14731473
@Field("firstname") String name;

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ static class Jedi {
694694
@Field("firstname") String name;
695695
}
696696

697-
@org.springframework.data.mongodb.core.mapping.Collation("de_AT")
697+
@org.springframework.data.mongodb.core.mapping.Document(collation = "de_AT")
698698
static class Sith {
699699

700700
@Field("firstname") String name;

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentEntityUnitTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,13 +304,13 @@ static class DocumentWithComposedAnnotation {}
304304
@Document("#{myProperty}")
305305
class MappedWithExtension {}
306306

307-
@Collation("#{myCollation}")
307+
@Document(collation = "#{myCollation}")
308308
class WithCollationFromSpEL {}
309309

310-
@Collation("en_US")
310+
@Document(collation = "en_US")
311311
class WithSimpleCollation {}
312312

313-
@Collation("{ 'locale' : 'en_US' }")
313+
@Document(collation = "{ 'locale' : 'en_US' }")
314314
class WithDocumentCollation {}
315315

316316
static class SampleExtension implements EvaluationContextExtension {

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/AbstractMongoQueryUnitTests.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
package org.springframework.data.mongodb.repository.query;
1717

1818
import static org.assertj.core.api.Assertions.*;
19-
import static org.mockito.ArgumentMatchers.*;
19+
import static org.mockito.ArgumentMatchers.any;
20+
import static org.mockito.ArgumentMatchers.anyString;
21+
import static org.mockito.ArgumentMatchers.eq;
2022
import static org.mockito.Mockito.*;
2123

2224
import java.lang.reflect.Method;
@@ -33,7 +35,6 @@
3335
import org.mockito.Mock;
3436
import org.mockito.Mockito;
3537
import org.mockito.junit.MockitoJUnitRunner;
36-
3738
import org.springframework.data.domain.Page;
3839
import org.springframework.data.domain.PageRequest;
3940
import org.springframework.data.domain.Pageable;
@@ -524,21 +525,21 @@ private interface Repo extends MongoRepository<Person, Long> {
524525
@org.springframework.data.mongodb.repository.Query(sort = "{ age : 1 }")
525526
List<Person> findByAge(Integer age, Sort page);
526527

527-
@org.springframework.data.mongodb.core.mapping.Collation(collation = "en_US")
528+
@org.springframework.data.mongodb.repository.Query(collation = "en_US")
528529
List<Person> findWithCollationUsingSpimpleStringValueByFirstName(String firstname);
529530

530-
@org.springframework.data.mongodb.core.mapping.Collation(collation = "{ 'locale' : 'en_US' }")
531+
@org.springframework.data.mongodb.repository.Query(collation = "{ 'locale' : 'en_US' }")
531532
List<Person> findWithCollationUsingDocumentByFirstName(String firstname);
532533

533-
@org.springframework.data.mongodb.core.mapping.Collation(collation = "?1")
534+
@org.springframework.data.mongodb.repository.Query(collation = "?1")
534535
List<Person> findWithCollationUsingPlaceholderByFirstName(String firstname, Object collation);
535536

536-
@org.springframework.data.mongodb.core.mapping.Collation(collation = "{ 'locale' : '?1' }")
537+
@org.springframework.data.mongodb.repository.Query(collation = "{ 'locale' : '?1' }")
537538
List<Person> findWithCollationUsingPlaceholderInDocumentByFirstName(String firstname, String collation);
538539

539540
List<Person> findWithCollationParameterByFirstName(String firstname, Collation collation);
540541

541-
@org.springframework.data.mongodb.core.mapping.Collation(collation = "{ 'locale' : 'en_US' }")
542+
@org.springframework.data.mongodb.repository.Query(collation = "{ 'locale' : 'en_US' }")
542543
List<Person> findWithWithCollationParameterAndAnnotationByFirstName(String firstname, Collation collation);
543544
}
544545

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/AbstractReactiveMongoQueryUnitTests.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.mockito.Mock;
3131
import org.mockito.Mockito;
3232
import org.mockito.junit.MockitoJUnitRunner;
33-
3433
import org.springframework.data.mongodb.core.Person;
3534
import org.springframework.data.mongodb.core.ReactiveFindOperation.FindWithQuery;
3635
import org.springframework.data.mongodb.core.ReactiveFindOperation.ReactiveFind;
@@ -281,25 +280,25 @@ public ReactiveMongoQueryFake setLimitingQuery(boolean limitingQuery) {
281280

282281
private interface Repo extends ReactiveMongoRepository<Person, Long> {
283282

284-
@org.springframework.data.mongodb.core.mapping.Collation("en_US")
283+
@org.springframework.data.mongodb.repository.Query(collation = "en_US")
285284
List<Person> findWithCollationUsingSpimpleStringValueByFirstName(String firstname);
286285

287-
@org.springframework.data.mongodb.core.mapping.Collation("{ 'locale' : 'en_US' }")
286+
@org.springframework.data.mongodb.repository.Query(collation = "{ 'locale' : 'en_US' }")
288287
List<Person> findWithCollationUsingDocumentByFirstName(String firstname);
289288

290-
@org.springframework.data.mongodb.core.mapping.Collation("?1")
289+
@org.springframework.data.mongodb.repository.Query(collation = "?1")
291290
List<Person> findWithCollationUsingPlaceholderByFirstName(String firstname, Object collation);
292291

293-
@org.springframework.data.mongodb.core.mapping.Collation("{ 'locale' : '?1' }")
292+
@org.springframework.data.mongodb.repository.Query(collation = "{ 'locale' : '?1' }")
294293
List<Person> findWithCollationUsingPlaceholderInDocumentByFirstName(String firstname, String collation);
295294

296-
@org.springframework.data.mongodb.core.mapping.Collation("{ 'locale' : '?1', 'strength' : ?#{[2]}}")
295+
@org.springframework.data.mongodb.repository.Query(collation = "{ 'locale' : '?1', 'strength' : ?#{[2]}}")
297296
List<Person> findWithCollationUsingPlaceholdersInDocumentByFirstName(String firstname, String collation,
298297
int strength);
299298

300299
List<Person> findWithCollationParameterByFirstName(String firstname, Collation collation);
301300

302-
@org.springframework.data.mongodb.core.mapping.Collation("{ 'locale' : 'en_US' }")
301+
@org.springframework.data.mongodb.repository.Query(collation = "{ 'locale' : 'en_US' }")
303302
List<Person> findWithWithCollationParameterAndAnnotationByFirstName(String firstname, Collation collation);
304303
}
305304
}

src/main/asciidoc/reference/mongodb.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,8 +1691,8 @@ Collations can be used to create collections and indexes. If you create a collec
16911691
collation is applied to index creation and queries unless you specify a different collation. A collation is valid for a
16921692
whole operation and cannot be specified on a per-field basis.
16931693

1694-
Like other metadata, collations can be be derived from the domain type via the `@Collation` annotation and will be applied
1695-
directly when executing queries, creating collections or indexes.
1694+
Like other metadata, collations can be be derived from the domain type via the `collation` attribute of the `@Document`
1695+
annotation and will be applied directly when executing queries, creating collections or indexes.
16961696

16971697
NOTE: Annotated collations will not be used when a collection is auto created by MongoDB on first interaction. This would
16981698
require additional store interaction delaying the entire process. Please use `MongoOperations.createCollection` for those cases.
@@ -1746,29 +1746,29 @@ WARNING: Indexes are only used if the collation used for the operation matches t
17461746

17471747
include::./mongo-json-schema.adoc[leveloffset=+1]
17481748

1749-
<<mongo.repositories>> support `Collations` via the `@org.springframework.data.mongodb.mapping.Collation` annotation.
1749+
<<mongo.repositories>> support `Collations` via the `collation` attribute of the `@Query` annotation.
17501750

17511751
.Collation support for Repositories
17521752
====
17531753
[source,java]
17541754
----
17551755
public interface PersonRepository extends MongoRepository<Person, String> {
17561756
1757-
@Collation("en_US") <1>
1757+
@Query(collation = "en_US") <1>
17581758
List<Person> findByFirstname(String firstname);
17591759
1760-
@Collation("{ 'locale' : 'en_US' }") <2>
1760+
@Query(collation = "{ 'locale' : 'en_US' }") <2>
17611761
List<Person> findPersonByFirstname(String firstname);
17621762
1763-
@Collation("?1") <3>
1763+
@Query(collation = "?1") <3>
17641764
List<Person> findByFirstname(String firstname, Object collation);
17651765
1766-
@Collation("{ 'locale' : '?1' }") <4>
1766+
@Query(collation = "{ 'locale' : '?1' }") <4>
17671767
List<Person> findByFirstname(String firstname, String collation);
17681768
17691769
List<Person> findByFirstname(String firstname, Collation collation); <5>
17701770
1771-
@Collation("{ 'locale' : 'en_US' }")
1771+
@Query(collation = "{ 'locale' : 'en_US' }")
17721772
List<Person> findByFirstname(String firstname, @Nullable Collation collation); <6>
17731773
}
17741774
----

0 commit comments

Comments
 (0)