Skip to content

DATAMONGO-1085 - Sort can not use the metamodel classes generated by QueryDSL. #236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.7.0.BUILD-SNAPSHOT</version>
<version>1.7.0.DATAMONGO-1085-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-mongodb-cross-store/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.7.0.BUILD-SNAPSHOT</version>
<version>1.7.0.DATAMONGO-1085-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.7.0.BUILD-SNAPSHOT</version>
<version>1.7.0.DATAMONGO-1085-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.7.0.BUILD-SNAPSHOT</version>
<version>1.7.0.DATAMONGO-1085-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-log4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.7.0.BUILD-SNAPSHOT</version>
<version>1.7.0.DATAMONGO-1085-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.7.0.BUILD-SNAPSHOT</version>
<version>1.7.0.DATAMONGO-1085-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1969,11 +1969,16 @@ private static final MongoConverter getDefaultMongoConverter(MongoDbFactory fact

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

if (query == null || query.getSortObject() == null) {
if (query == null) {
return null;
}

DBObject sortObject = query.getSortObject();
if (sortObject == null) {
return null;
}

return queryMapper.getMappedSort(query.getSortObject(), mappingContext.getPersistentEntity(type));
return queryMapper.getMappedSort(sortObject, mappingContext.getPersistentEntity(type));
}

// Callback implementations
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2012 the original author or authors.
* Copyright 2011-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +27,7 @@
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.querydsl.EntityPathResolver;
import org.springframework.data.querydsl.QSort;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import org.springframework.data.querydsl.SimpleEntityPathResolver;
import org.springframework.data.repository.core.EntityInformation;
Expand All @@ -44,6 +45,7 @@
* Special QueryDsl based repository implementation that allows execution {@link Predicate}s in various forms.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleMongoRepository<T, ID> implements
QueryDslPredicateExecutor<T> {
Expand Down Expand Up @@ -105,7 +107,6 @@ public List<T> findAll(Predicate predicate) {
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.mysema.query.types.Predicate, com.mysema.query.types.OrderSpecifier<?>[])
*/
public List<T> findAll(Predicate predicate, OrderSpecifier<?>... orders) {

return createQueryFor(predicate).orderBy(orders).list();
}

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

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

/*
* (non-Javadoc)
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(org.springframework.data.domain.Pageable)
*/
public Page<T> findAll(Pageable pageable) {

MongodbQuery<T> countQuery = createQuery();
MongodbQuery<T> query = createQuery();

return new PageImpl<T>(applyPagination(query, pageable).list(), pageable, countQuery.count());
}

/* (non-Javadoc)
* @see org.springframework.data.mongodb.repository.support.SimpleMongoRepository#findAll(org.springframework.data.domain.Sort)
*/
public List<T> findAll(Sort sort) {
return applySorting(createQuery(), sort).list();
}

/*
* (non-Javadoc)
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#count(com.mysema.query.types.Predicate)
Expand All @@ -144,12 +164,17 @@ public long count(Predicate predicate) {
* @param predicate
* @return
*/
private MongodbQuery<T> createQueryFor(Predicate... predicate) {

Class<T> domainType = entityInformation.getJavaType();
private MongodbQuery<T> createQueryFor(Predicate predicate) {
return createQuery().where(predicate);
}

MongodbQuery<T> query = new SpringDataMongodbQuery<T>(mongoOperations, domainType);
return query.where(predicate);
/**
* Creates a {@link MongodbQuery}.
*
* @return
*/
private MongodbQuery<T> createQuery() {
return new SpringDataMongodbQuery<T>(mongoOperations, entityInformation.getJavaType());
}

/**
Expand Down Expand Up @@ -182,6 +207,14 @@ private MongodbQuery<T> applySorting(MongodbQuery<T> query, Sort sort) {
return query;
}

if (sort instanceof QSort) {

List<OrderSpecifier<?>> orderSpecifiers = ((QSort) sort).getOrderSpecifiers();
query.orderBy(orderSpecifiers.toArray(new OrderSpecifier<?>[orderSpecifiers.size()]));

return query;
}

for (Order order : sort) {
query.orderBy(toOrder(order));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.repository.Person.Sex;
import org.springframework.data.querydsl.QSort;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.google.common.collect.Lists;

/**
* Base class for tests for {@link PersonRepository}.
*
Expand Down Expand Up @@ -1071,4 +1074,77 @@ public void returnsOrderedResultsForQuerydslOrderSpecifier() {

assertThat(result, contains(alicia, boyd, carter, dave, leroi, oliver, stefan));
}

/**
* @see DATAMONGO-1085
*/
@Test
public void shouldSupportSortingByQueryDslOrderSpecifier() {

repository.deleteAll();

List<Person> persons = new ArrayList<Person>();
for (int i = 0; i < 3; i++) {

Person pers = new Person(String.format("Siggi %s", i), "Bar", 30);
pers.setAddress(new Address(String.format("Street %s", i), "12345", "SinCity"));

persons.add(pers);
}

repository.save(persons);

QPerson person = QPerson.person;

List<Person> result = Lists.newArrayList(repository.findAll(person.firstname.isNotNull(),
person.address.street.desc()));

assertThat(result, hasSize(persons.size()));
assertThat(result.get(0).getFirstname(), is(persons.get(2).getFirstname()));
}

/**
* @see DATAMONGO-1085
*/
@Test
public void shouldSupportSortingWithQSortByQueryDslOrderSpecifier() throws Exception {

repository.deleteAll();

List<Person> persons = new ArrayList<Person>();
for (int i = 0; i < 3; i++) {
Person pers = new Person(String.format("Siggi %s", i), "Bar", 30);
pers.setAddress(new Address(String.format("Street %s", i), "12345", "SinCity"));
persons.add(pers);
}
repository.save(persons);

PageRequest pageRequest = new PageRequest(0, 2, new QSort(person.address.street.desc()));
List<Person> result = Lists.newArrayList(repository.findAll(pageRequest));

assertThat(result, hasSize(pageRequest.getPageSize()));
assertThat(result.get(0).getFirstname(), is("Siggi 2"));
}

/**
* @see DATAMONGO-1085
*/
@Test
public void shouldSupportSortingWithQSort() throws Exception {

repository.deleteAll();

List<Person> persons = new ArrayList<Person>();
for (int i = 0; i < 3; i++) {
Person pers = new Person(String.format("Siggi %s", i), "Bar", 30);
pers.setAddress(new Address(String.format("Street %s", i), "12345", "SinCity"));
persons.add(pers);
}
repository.save(persons);

List<Person> result = Lists.newArrayList(repository.findAll(new QSort(person.address.street.desc())));

assertThat(result, hasSize(persons.size()));
assertThat(result.get(0).getFirstname(), is("Siggi 2"));
}
}