Skip to content

Commit a33fb26

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 cf2707f commit a33fb26

File tree

3 files changed

+94
-10
lines changed

3 files changed

+94
-10
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
@@ -1969,11 +1969,16 @@ private static final MongoConverter getDefaultMongoConverter(MongoDbFactory fact
19691969

19701970
private DBObject getMappedSortObject(Query query, Class<?> type) {
19711971

1972-
if (query == null || query.getSortObject() == null) {
1972+
if (query == null) {
1973+
return null;
1974+
}
1975+
1976+
DBObject sortObject = query.getSortObject();
1977+
if (sortObject == null) {
19731978
return null;
19741979
}
19751980

1976-
return queryMapper.getMappedSort(query.getSortObject(), mappingContext.getPersistentEntity(type));
1981+
return queryMapper.getMappedSort(sortObject, mappingContext.getPersistentEntity(type));
19771982
}
19781983

19791984
// Callback implementations

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

Lines changed: 41 additions & 8 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.EntityInformation;
@@ -44,6 +45,7 @@
4445
* Special QueryDsl based repository implementation that allows execution {@link Predicate}s in various forms.
4546
*
4647
* @author Oliver Gierke
48+
* @author Thomas Darimont
4749
*/
4850
public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleMongoRepository<T, ID> implements
4951
QueryDslPredicateExecutor<T> {
@@ -105,7 +107,6 @@ public List<T> findAll(Predicate predicate) {
105107
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.mysema.query.types.Predicate, com.mysema.query.types.OrderSpecifier<?>[])
106108
*/
107109
public List<T> findAll(Predicate predicate, OrderSpecifier<?>... orders) {
108-
109110
return createQueryFor(predicate).orderBy(orders).list();
110111
}
111112

@@ -115,7 +116,7 @@ public List<T> findAll(Predicate predicate, OrderSpecifier<?>... orders) {
115116
*/
116117
@Override
117118
public Iterable<T> findAll(OrderSpecifier<?>... orders) {
118-
return createQueryFor(new Predicate[0]).orderBy(orders).list();
119+
return createQuery().orderBy(orders).list();
119120
}
120121

121122
/*
@@ -130,6 +131,25 @@ public Page<T> findAll(Predicate predicate, Pageable pageable) {
130131
return new PageImpl<T>(applyPagination(query, pageable).list(), pageable, countQuery.count());
131132
}
132133

134+
/*
135+
* (non-Javadoc)
136+
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.mysema.query.types.Predicate, org.springframework.data.domain.Pageable)
137+
*/
138+
public Page<T> findAll(Pageable pageable) {
139+
140+
MongodbQuery<T> countQuery = createQuery();
141+
MongodbQuery<T> query = createQuery();
142+
143+
return new PageImpl<T>(applyPagination(query, pageable).list(), pageable, countQuery.count());
144+
}
145+
146+
/* (non-Javadoc)
147+
* @see org.springframework.data.mongodb.repository.support.SimpleMongoRepository#findAll(org.springframework.data.domain.Sort)
148+
*/
149+
public List<T> findAll(Sort sort) {
150+
return applySorting(createQuery(), sort).list();
151+
}
152+
133153
/*
134154
* (non-Javadoc)
135155
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#count(com.mysema.query.types.Predicate)
@@ -144,12 +164,17 @@ public long count(Predicate predicate) {
144164
* @param predicate
145165
* @return
146166
*/
147-
private MongodbQuery<T> createQueryFor(Predicate... predicate) {
148-
149-
Class<T> domainType = entityInformation.getJavaType();
167+
private MongodbQuery<T> createQueryFor(Predicate predicate) {
168+
return createQuery().where(predicate);
169+
}
150170

151-
MongodbQuery<T> query = new SpringDataMongodbQuery<T>(mongoOperations, domainType);
152-
return query.where(predicate);
171+
/**
172+
* Creates a {@link MongodbQuery}.
173+
*
174+
* @return
175+
*/
176+
private MongodbQuery<T> createQuery() {
177+
return new SpringDataMongodbQuery<T>(mongoOperations, entityInformation.getJavaType());
153178
}
154179

155180
/**
@@ -182,6 +207,14 @@ private MongodbQuery<T> applySorting(MongodbQuery<T> query, Sort sort) {
182207
return query;
183208
}
184209

210+
if (sort instanceof QSort) {
211+
212+
List<OrderSpecifier<?>> orderSpecifiers = ((QSort) sort).getOrderSpecifiers();
213+
query.orderBy(orderSpecifiers.toArray(new OrderSpecifier<?>[orderSpecifiers.size()]));
214+
215+
return query;
216+
}
217+
185218
for (Order order : sort) {
186219
query.orderBy(toOrder(order));
187220
}

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;
@@ -1101,4 +1102,49 @@ public void shouldSupportSortingByQueryDslOrderSpecifier() {
11011102
assertThat(result, hasSize(persons.size()));
11021103
assertThat(result.get(0).getFirstname(), is(persons.get(2).getFirstname()));
11031104
}
1105+
1106+
/**
1107+
* @see DATAMONGO-1085
1108+
*/
1109+
@Test
1110+
public void shouldSupportSortingWithQSortByQueryDslOrderSpecifier() throws Exception {
1111+
1112+
repository.deleteAll();
1113+
1114+
List<Person> persons = new ArrayList<Person>();
1115+
for (int i = 0; i < 3; i++) {
1116+
Person pers = new Person(String.format("Siggi %s", i), "Bar", 30);
1117+
pers.setAddress(new Address(String.format("Street %s", i), "12345", "SinCity"));
1118+
persons.add(pers);
1119+
}
1120+
repository.save(persons);
1121+
1122+
PageRequest pageRequest = new PageRequest(0, 2, new QSort(person.address.street.desc()));
1123+
List<Person> result = Lists.newArrayList(repository.findAll(pageRequest));
1124+
1125+
assertThat(result, hasSize(pageRequest.getPageSize()));
1126+
assertThat(result.get(0).getFirstname(), is("Siggi 2"));
1127+
}
1128+
1129+
/**
1130+
* @see DATAMONGO-1085
1131+
*/
1132+
@Test
1133+
public void shouldSupportSortingWithQSort() throws Exception {
1134+
1135+
repository.deleteAll();
1136+
1137+
List<Person> persons = new ArrayList<Person>();
1138+
for (int i = 0; i < 3; i++) {
1139+
Person pers = new Person(String.format("Siggi %s", i), "Bar", 30);
1140+
pers.setAddress(new Address(String.format("Street %s", i), "12345", "SinCity"));
1141+
persons.add(pers);
1142+
}
1143+
repository.save(persons);
1144+
1145+
List<Person> result = Lists.newArrayList(repository.findAll(new QSort(person.address.street.desc())));
1146+
1147+
assertThat(result, hasSize(persons.size()));
1148+
assertThat(result.get(0).getFirstname(), is("Siggi 2"));
1149+
}
11041150
}

0 commit comments

Comments
 (0)