Skip to content

Commit 02926e5

Browse files
author
Thomas Darimont
committed
DATAMONGO-1085 - Fixed sorting with querydsl in QueryDslMongoRepository.
We now translate QSort's OrderSpecifiers into appropriate sort criteria. Previously the OrderSpecificers were not correctly translated to appropriate property path expressions. Added support for findAll(Pageable) and findAll(Sort) to QueryDslMongoRepository. Avoid multiple creations of sortObjects in MongoTemplate.getMappedSortObject(..).
1 parent d55f756 commit 02926e5

File tree

4 files changed

+106
-9
lines changed

4 files changed

+106
-9
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,11 +1966,16 @@ private static final MongoConverter getDefaultMongoConverter(MongoDbFactory fact
19661966

19671967
private DBObject getMappedSortObject(Query query, Class<?> type) {
19681968

1969-
if (query == null || query.getSortObject() == null) {
1969+
if (query == null) {
1970+
return null;
1971+
}
1972+
1973+
DBObject sortObject = query.getSortObject();
1974+
if (sortObject == null) {
19701975
return null;
19711976
}
19721977

1973-
return queryMapper.getMappedSort(query.getSortObject(), mappingContext.getPersistentEntity(type));
1978+
return queryMapper.getMappedSort(sortObject, mappingContext.getPersistentEntity(type));
19741979
}
19751980

19761981
// Callback implementations

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

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2012 the original author or authors.
2+
* Copyright 2011-2014 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.
@@ -27,6 +27,7 @@
2727
import org.springframework.data.mongodb.core.MongoTemplate;
2828
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
2929
import org.springframework.data.querydsl.EntityPathResolver;
30+
import org.springframework.data.querydsl.QSort;
3031
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
3132
import org.springframework.data.querydsl.SimpleEntityPathResolver;
3233
import org.springframework.data.repository.core.EntityMetadata;
@@ -43,6 +44,7 @@
4344
* Special QueryDsl based repository implementation that allows execution {@link Predicate}s in various forms.
4445
*
4546
* @author Oliver Gierke
47+
* @author Thomas Darimont
4648
*/
4749
public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleMongoRepository<T, ID> implements
4850
QueryDslPredicateExecutor<T> {
@@ -98,7 +100,6 @@ public List<T> findAll(Predicate predicate) {
98100
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.mysema.query.types.Predicate, com.mysema.query.types.OrderSpecifier<?>[])
99101
*/
100102
public List<T> findAll(Predicate predicate, OrderSpecifier<?>... orders) {
101-
102103
return createQueryFor(predicate).orderBy(orders).list();
103104
}
104105

@@ -114,6 +115,25 @@ public Page<T> findAll(Predicate predicate, Pageable pageable) {
114115
return new PageImpl<T>(applyPagination(query, pageable).list(), pageable, countQuery.count());
115116
}
116117

118+
/*
119+
* (non-Javadoc)
120+
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.mysema.query.types.Predicate, org.springframework.data.domain.Pageable)
121+
*/
122+
public Page<T> findAll(Pageable pageable) {
123+
124+
MongodbQuery<T> countQuery = createQuery();
125+
MongodbQuery<T> query = createQuery();
126+
127+
return new PageImpl<T>(applyPagination(query, pageable).list(), pageable, countQuery.count());
128+
}
129+
130+
/* (non-Javadoc)
131+
* @see org.springframework.data.mongodb.repository.support.SimpleMongoRepository#findAll(org.springframework.data.domain.Sort)
132+
*/
133+
public List<T> findAll(Sort sort) {
134+
return applySorting(createQuery(), sort).list();
135+
}
136+
117137
/*
118138
* (non-Javadoc)
119139
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#count(com.mysema.query.types.Predicate)
@@ -129,11 +149,11 @@ public long count(Predicate predicate) {
129149
* @return
130150
*/
131151
private MongodbQuery<T> createQueryFor(Predicate predicate) {
152+
return createQuery().where(predicate);
153+
}
132154

133-
Class<T> domainType = getEntityInformation().getJavaType();
134-
135-
MongodbQuery<T> query = new SpringDataMongodbQuery<T>(getMongoOperations(), domainType);
136-
return query.where(predicate);
155+
private MongodbQuery<T> createQuery() {
156+
return new SpringDataMongodbQuery<T>(getMongoOperations(), getEntityInformation().getJavaType());
137157
}
138158

139159
/**
@@ -166,6 +186,14 @@ private MongodbQuery<T> applySorting(MongodbQuery<T> query, Sort sort) {
166186
return query;
167187
}
168188

189+
if (sort instanceof QSort) {
190+
191+
List<OrderSpecifier<?>> orderSpecifiers = ((QSort) sort).getOrderSpecifiers();
192+
query.orderBy(orderSpecifiers.toArray(new OrderSpecifier<?>[orderSpecifiers.size()]));
193+
194+
return query;
195+
}
196+
169197
for (Order order : sort) {
170198
query.orderBy(toOrder(order));
171199
}

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.springframework.data.mongodb.core.MongoOperations;
4848
import org.springframework.data.mongodb.core.query.BasicQuery;
4949
import org.springframework.data.mongodb.repository.Person.Sex;
50+
import org.springframework.data.querydsl.QSort;
5051
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
5152

5253
import com.google.common.collect.Lists;
@@ -1090,4 +1091,49 @@ public void shouldSupportSortingByQueryDslOrderSpecifier() {
10901091
assertThat(result, hasSize(persons.size()));
10911092
assertThat(result.get(0).getFirstname(), is(persons.get(2).getFirstname()));
10921093
}
1094+
1095+
/**
1096+
* @see DATAMONGO-1085
1097+
*/
1098+
@Test
1099+
public void shouldSupportSortingWithQSortByQueryDslOrderSpecifier() throws Exception {
1100+
1101+
repository.deleteAll();
1102+
1103+
List<Person> persons = new ArrayList<Person>();
1104+
for (int i = 0; i < 3; i++) {
1105+
Person pers = new Person(String.format("Siggi %s", i), "Bar", 30);
1106+
pers.setAddress(new Address(String.format("Street %s", i), "12345", "SinCity"));
1107+
persons.add(pers);
1108+
}
1109+
repository.save(persons);
1110+
1111+
PageRequest pageRequest = new PageRequest(0, 2, new QSort(person.address.street.desc()));
1112+
List<Person> result = Lists.newArrayList(repository.findAll(pageRequest));
1113+
1114+
assertThat(result, hasSize(pageRequest.getPageSize()));
1115+
assertThat(result.get(0).getFirstname(), is("Siggi 2"));
1116+
}
1117+
1118+
/**
1119+
* @see DATAMONGO-1085
1120+
*/
1121+
@Test
1122+
public void shouldSupportSortingWithQSort() throws Exception {
1123+
1124+
repository.deleteAll();
1125+
1126+
List<Person> persons = new ArrayList<Person>();
1127+
for (int i = 0; i < 3; i++) {
1128+
Person pers = new Person(String.format("Siggi %s", i), "Bar", 30);
1129+
pers.setAddress(new Address(String.format("Street %s", i), "12345", "SinCity"));
1130+
persons.add(pers);
1131+
}
1132+
repository.save(persons);
1133+
1134+
List<Person> result = Lists.newArrayList(repository.findAll(new QSort(person.address.street.desc())));
1135+
1136+
assertThat(result, hasSize(persons.size()));
1137+
assertThat(result.get(0).getFirstname(), is("Siggi 2"));
1138+
}
10931139
}

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,22 @@
2424
* @author Thomas Darimont
2525
*/
2626
@ContextConfiguration
27-
public class PersonRepositoryIntegrationTests extends AbstractPersonRepositoryIntegrationTests {}
27+
public class PersonRepositoryIntegrationTests extends AbstractPersonRepositoryIntegrationTests {
28+
@Override
29+
public void shouldSupportSortingWithQSortByQueryDslOrderSpecifier() throws Exception {
30+
// TODO Auto-generated method stub
31+
super.shouldSupportSortingWithQSortByQueryDslOrderSpecifier();
32+
}
33+
34+
@Override
35+
public void shouldSupportSortingByQueryDslOrderSpecifier() {
36+
// TODO Auto-generated method stub
37+
super.shouldSupportSortingByQueryDslOrderSpecifier();
38+
}
39+
40+
@Override
41+
public void shouldSupportSortingWithQSort() throws Exception {
42+
// TODO Auto-generated method stub
43+
super.shouldSupportSortingWithQSort();
44+
}
45+
}

0 commit comments

Comments
 (0)