Skip to content

Commit 24cbef9

Browse files
Thomas Darimontodrotbohm
Thomas Darimont
authored andcommitted
DATAMONGO-1085 - Fixed sorting with Querydsl in QueryDslMongoRepository.
We now translate QSort's OrderSpecifiers into appropriate sort criteria. Previously the OrderSpecifiers were not correctly translated to appropriate property path expressions. We're now overriding support for findAll(Pageable) and findAll(Sort) to QueryDslMongoRepository to apply special QSort handling. Original pull request: #236.
1 parent b5f7444 commit 24cbef9

File tree

2 files changed

+120
-6
lines changed

2 files changed

+120
-6
lines changed

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

Lines changed: 43 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.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

@@ -121,6 +122,28 @@ public Page<T> findAll(Predicate predicate, Pageable pageable) {
121122
return new PageImpl<T>(applyPagination(query, pageable).list(), pageable, countQuery.count());
122123
}
123124

125+
/*
126+
* (non-Javadoc)
127+
* @see org.springframework.data.mongodb.repository.support.SimpleMongoRepository#findAll(org.springframework.data.domain.Pageable)
128+
*/
129+
@Override
130+
public Page<T> findAll(Pageable pageable) {
131+
132+
MongodbQuery<T> countQuery = createQuery();
133+
MongodbQuery<T> query = createQuery();
134+
135+
return new PageImpl<T>(applyPagination(query, pageable).list(), pageable, countQuery.count());
136+
}
137+
138+
/*
139+
* (non-Javadoc)
140+
* @see org.springframework.data.mongodb.repository.support.SimpleMongoRepository#findAll(org.springframework.data.domain.Sort)
141+
*/
142+
@Override
143+
public List<T> findAll(Sort sort) {
144+
return applySorting(createQuery(), sort).list();
145+
}
146+
124147
/*
125148
* (non-Javadoc)
126149
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#count(com.mysema.query.types.Predicate)
@@ -136,11 +159,16 @@ public long count(Predicate predicate) {
136159
* @return
137160
*/
138161
private MongodbQuery<T> createQueryFor(Predicate predicate) {
162+
return createQuery().where(predicate);
163+
}
139164

140-
Class<T> domainType = entityInformation.getJavaType();
141-
142-
MongodbQuery<T> query = new SpringDataMongodbQuery<T>(mongoOperations, domainType);
143-
return query.where(predicate);
165+
/**
166+
* Creates a {@link MongodbQuery}.
167+
*
168+
* @return
169+
*/
170+
private MongodbQuery<T> createQuery() {
171+
return new SpringDataMongodbQuery<T>(mongoOperations, entityInformation.getJavaType());
144172
}
145173

146174
/**
@@ -173,6 +201,15 @@ private MongodbQuery<T> applySorting(MongodbQuery<T> query, Sort sort) {
173201
return query;
174202
}
175203

204+
// TODO: find better solution than instanceof check
205+
if (sort instanceof QSort) {
206+
207+
List<OrderSpecifier<?>> orderSpecifiers = ((QSort) sort).getOrderSpecifiers();
208+
query.orderBy(orderSpecifiers.toArray(new OrderSpecifier<?>[orderSpecifiers.size()]));
209+
210+
return query;
211+
}
212+
176213
for (Order order : sort) {
177214
query.orderBy(toOrder(order));
178215
}

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.HashSet;
2525
import java.util.List;
2626

27+
import org.hamcrest.Matchers;
2728
import org.junit.Before;
2829
import org.junit.Ignore;
2930
import org.junit.Test;
@@ -47,6 +48,7 @@
4748
import org.springframework.data.mongodb.core.MongoOperations;
4849
import org.springframework.data.mongodb.core.query.BasicQuery;
4950
import org.springframework.data.mongodb.repository.Person.Sex;
51+
import org.springframework.data.querydsl.QSort;
5052
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
5153

5254
/**
@@ -1060,4 +1062,79 @@ public void shouldBindPlaceholdersUsedAsKeysCorrectly() {
10601062
assertThat(persons, hasSize(1));
10611063
assertThat(persons, hasItem(alicia));
10621064
}
1065+
1066+
/**
1067+
* @see DATAMONGO-1085
1068+
*/
1069+
@Test
1070+
public void shouldSupportSortingByQueryDslOrderSpecifier() {
1071+
1072+
repository.deleteAll();
1073+
1074+
List<Person> persons = new ArrayList<Person>();
1075+
1076+
for (int i = 0; i < 3; i++) {
1077+
Person person = new Person(String.format("Siggi %s", i), "Bar", 30);
1078+
person.setAddress(new Address(String.format("Street %s", i), "12345", "SinCity"));
1079+
persons.add(person);
1080+
}
1081+
1082+
repository.save(persons);
1083+
1084+
QPerson person = QPerson.person;
1085+
1086+
Iterable<Person> result = repository.findAll(person.firstname.isNotNull(), person.address.street.desc());
1087+
1088+
assertThat(result, is(Matchers.<Person> iterableWithSize(persons.size())));
1089+
assertThat(result.iterator().next().getFirstname(), is(persons.get(2).getFirstname()));
1090+
}
1091+
1092+
/**
1093+
* @see DATAMONGO-1085
1094+
*/
1095+
@Test
1096+
public void shouldSupportSortingWithQSortByQueryDslOrderSpecifier() throws Exception {
1097+
1098+
repository.deleteAll();
1099+
1100+
List<Person> persons = new ArrayList<Person>();
1101+
1102+
for (int i = 0; i < 3; i++) {
1103+
Person person = new Person(String.format("Siggi %s", i), "Bar", 30);
1104+
person.setAddress(new Address(String.format("Street %s", i), "12345", "SinCity"));
1105+
persons.add(person);
1106+
}
1107+
1108+
repository.save(persons);
1109+
1110+
PageRequest pageRequest = new PageRequest(0, 2, new QSort(person.address.street.desc()));
1111+
Iterable<Person> result = repository.findAll(pageRequest);
1112+
1113+
assertThat(result, is(Matchers.<Person> iterableWithSize(2)));
1114+
assertThat(result.iterator().next().getFirstname(), is("Siggi 2"));
1115+
}
1116+
1117+
/**
1118+
* @see DATAMONGO-1085
1119+
*/
1120+
@Test
1121+
public void shouldSupportSortingWithQSort() throws Exception {
1122+
1123+
repository.deleteAll();
1124+
1125+
List<Person> persons = new ArrayList<Person>();
1126+
1127+
for (int i = 0; i < 3; i++) {
1128+
Person person = new Person(String.format("Siggi %s", i), "Bar", 30);
1129+
person.setAddress(new Address(String.format("Street %s", i), "12345", "SinCity"));
1130+
persons.add(person);
1131+
}
1132+
1133+
repository.save(persons);
1134+
1135+
Iterable<Person> result = repository.findAll(new QSort(person.address.street.desc()));
1136+
1137+
assertThat(result, is(Matchers.<Person> iterableWithSize(persons.size())));
1138+
assertThat(result.iterator().next().getFirstname(), is("Siggi 2"));
1139+
}
10631140
}

0 commit comments

Comments
 (0)