Skip to content

Commit cce2bba

Browse files
DATAMONGO-888 - Mapping should be applied to Sort.
Query.getSortObject() is passed through QueryMapper in order to retrieve mapped field values. Original Pull Request: #162
1 parent a2cbcf6 commit cce2bba

File tree

6 files changed

+20
-291
lines changed

6 files changed

+20
-291
lines changed

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,10 @@
6666
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
6767
import org.springframework.data.mongodb.core.convert.DbRefResolver;
6868
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
69-
import org.springframework.data.mongodb.core.convert.EntityBackedSortConverter;
7069
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
7170
import org.springframework.data.mongodb.core.convert.MongoConverter;
7271
import org.springframework.data.mongodb.core.convert.MongoWriter;
7372
import org.springframework.data.mongodb.core.convert.QueryMapper;
74-
import org.springframework.data.mongodb.core.convert.SortConverter;
7573
import org.springframework.data.mongodb.core.convert.UpdateMapper;
7674
import org.springframework.data.mongodb.core.geo.GeoResults;
7775
import org.springframework.data.mongodb.core.index.MongoMappingEventPublisher;
@@ -357,7 +355,7 @@ protected void logCommandExecutionError(final DBObject command, CommandResult re
357355
}
358356

359357
public void executeQuery(Query query, String collectionName, DocumentCallbackHandler dch) {
360-
executeQuery(query, collectionName, dch, new QueryCursorPreparer(query, new SortConverter()));
358+
executeQuery(query, collectionName, dch, new QueryCursorPreparer(query, null));
361359
}
362360

363361
/**
@@ -535,7 +533,7 @@ public <T> List<T> find(final Query query, Class<T> entityClass, String collecti
535533
}
536534

537535
return doFind(collectionName, query.getQueryObject(), query.getFieldsObject(), entityClass,
538-
new QueryCursorPreparer(query, new EntityBackedSortConverter(mappingContext.getPersistentEntity(entityClass))));
536+
new QueryCursorPreparer(query, mappingContext.getPersistentEntity(entityClass)));
539537
}
540538

541539
public <T> T findById(Object id, Class<T> entityClass) {
@@ -618,8 +616,7 @@ public <T> T findAndModify(Query query, Update update, FindAndModifyOptions opti
618616
public <T> T findAndModify(Query query, Update update, FindAndModifyOptions options, Class<T> entityClass,
619617
String collectionName) {
620618
return doFindAndModify(collectionName, query.getQueryObject(), query.getFieldsObject(),
621-
new EntityBackedSortConverter(mappingContext.getPersistentEntity(entityClass)).convert(query.getSort()),
622-
entityClass, update, options);
619+
getMappedSortObject(query, mappingContext.getPersistentEntity(entityClass)), entityClass, update, options);
623620
}
624621

625622
// Find methods that take a Query to express the query and that return a single object that is also removed from the
@@ -632,8 +629,7 @@ public <T> T findAndRemove(Query query, Class<T> entityClass) {
632629
public <T> T findAndRemove(Query query, Class<T> entityClass, String collectionName) {
633630

634631
return doFindAndRemove(collectionName, query.getQueryObject(), query.getFieldsObject(),
635-
new EntityBackedSortConverter(mappingContext.getPersistentEntity(entityClass)).convert(query.getSort()),
636-
entityClass);
632+
getMappedSortObject(query, mappingContext.getPersistentEntity(entityClass)), entityClass);
637633
}
638634

639635
public long count(Query query, Class<?> entityClass) {
@@ -1947,6 +1943,15 @@ private static final MongoConverter getDefaultMongoConverter(MongoDbFactory fact
19471943
return converter;
19481944
}
19491945

1946+
private DBObject getMappedSortObject(Query query, MongoPersistentEntity<?> entity) {
1947+
1948+
if (query == null || query.getSortObject() == null) {
1949+
return null;
1950+
}
1951+
1952+
return queryMapper.getMappedObject(query.getSortObject(), entity);
1953+
}
1954+
19501955
// Callback implementations
19511956

19521957
/**
@@ -2140,13 +2145,12 @@ public WriteConcern resolve(MongoAction action) {
21402145
class QueryCursorPreparer implements CursorPreparer {
21412146

21422147
private final Query query;
2143-
private final SortConverter sortConverter;
2148+
private final MongoPersistentEntity<?> entity;
21442149

2145-
public QueryCursorPreparer(Query query, SortConverter sortConverter) {
2150+
public QueryCursorPreparer(Query query, MongoPersistentEntity<?> entity) {
21462151

2147-
Assert.notNull(sortConverter, "SortConverter must not be null");
21482152
this.query = query;
2149-
this.sortConverter = sortConverter;
2153+
this.entity = entity;
21502154
}
21512155

21522156
/*
@@ -2173,8 +2177,8 @@ public DBCursor prepare(DBCursor cursor) {
21732177
if (query.getLimit() > 0) {
21742178
cursorToUse = cursorToUse.limit(query.getLimit());
21752179
}
2176-
if (query.getSort() != null) {
2177-
cursorToUse = cursorToUse.sort(sortConverter.convert(query.getSort()));
2180+
if (query.getSortObject() != null) {
2181+
cursorToUse = cursorToUse.sort(getMappedSortObject(query, entity));
21782182
}
21792183
if (StringUtils.hasText(query.getHint())) {
21802184
cursorToUse = cursorToUse.hint(query.getHint());

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/EntityBackedSortConverter.java

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

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/SortConverter.java

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

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
* @author Thomas Risberg
4040
* @author Oliver Gierke
4141
* @author Thomas Darimont
42-
* @author Christoph Strobl
4342
*/
4443
public class Query {
4544

@@ -229,12 +228,6 @@ public DBObject getFieldsObject() {
229228
return this.fieldSpec == null ? null : fieldSpec.getFieldsObject();
230229
}
231230

232-
/**
233-
* @return
234-
* @deprecated in 1.4.2. Use {@link org.springframework.data.mongodb.core.convert.SortConverter} and
235-
* {@link #getSort()}.
236-
*/
237-
@Deprecated
238231
public DBObject getSortObject() {
239232

240233
if (this.sort == null) {
@@ -250,14 +243,6 @@ public DBObject getSortObject() {
250243
return dbo;
251244
}
252245

253-
/**
254-
* @return
255-
* @since 1.4.2
256-
*/
257-
public Sort getSort() {
258-
return this.sort;
259-
}
260-
261246
/**
262247
* Get the number of documents to skip.
263248
*

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2014 the original author or authors.
2+
* Copyright 2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,7 +25,6 @@
2525
import org.mockito.runners.MockitoJUnitRunner;
2626
import org.springframework.data.mongodb.MongoDbFactory;
2727
import org.springframework.data.mongodb.core.MongoTemplate.QueryCursorPreparer;
28-
import org.springframework.data.mongodb.core.convert.SortConverter;
2928
import org.springframework.data.mongodb.core.query.Query;
3029

3130
import com.mongodb.DBCursor;
@@ -34,7 +33,6 @@
3433
* Unit tests for {@link QueryCursorPreparer}.
3534
*
3635
* @author Oliver Gierke
37-
* @author Christoph Strobl
3836
*/
3937
@RunWith(MockitoJUnitRunner.class)
4038
public class QueryCursorPreparerUnitTests {
@@ -50,7 +48,7 @@ public void appliesHintsCorrectly() {
5048

5149
Query query = query(where("foo").is("bar")).withHint("hint");
5250

53-
CursorPreparer preparer = new MongoTemplate(factory).new QueryCursorPreparer(query, new SortConverter());
51+
CursorPreparer preparer = new MongoTemplate(factory).new QueryCursorPreparer(query, null);
5452
preparer.prepare(cursor);
5553

5654
verify(cursor).hint("hint");

0 commit comments

Comments
 (0)