From ebb2be942a8c087b709d425d6a85c6a17c4ef44e Mon Sep 17 00:00:00 2001 From: Thomas Darimont Date: Tue, 26 Aug 2014 13:44:29 +0200 Subject: [PATCH] DATAJPA-596 - Allow sorting by Querydsl operator expression. Added test case for sorting by Querydsl operator expression. Original pull request: #107. --- .../data/jpa/domain/sample/User.java | 10 ++++++++ .../support/QueryDslJpaRepositoryTests.java | 23 +++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/springframework/data/jpa/domain/sample/User.java b/src/test/java/org/springframework/data/jpa/domain/sample/User.java index a2b60d16a..94c7e213c 100644 --- a/src/test/java/org/springframework/data/jpa/domain/sample/User.java +++ b/src/test/java/org/springframework/data/jpa/domain/sample/User.java @@ -84,6 +84,8 @@ public class User { @Lob private byte[] binaryData; @ElementCollection private Set attributes; + + @Temporal(TemporalType.DATE) private Date dateOfBirth; /** * Creates a new empty instance of {@code User}. @@ -365,6 +367,14 @@ public class User { public void setAttributes(Set attributes) { this.attributes = attributes; } + + public Date getDateOfBirth() { + return dateOfBirth; + } + + public void setDateOfBirth(Date dateOfBirth) { + this.dateOfBirth = dateOfBirth; + } /* * (non-Javadoc) diff --git a/src/test/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepositoryTests.java b/src/test/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepositoryTests.java index 1b164131d..0524f9730 100644 --- a/src/test/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepositoryTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepositoryTests.java @@ -15,14 +15,17 @@ */ package org.springframework.data.jpa.repository.support; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; +import static org.hamcrest.Matchers.hasItems; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; +import org.joda.time.LocalDate; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -309,4 +312,20 @@ public class QueryDslJpaRepositoryTests { assertThat(users, hasSize(3)); assertThat(users, hasItems(dave, oliver, carter)); } + + /** + * @DATAJPA-566 + */ + @Test + public void shouldSupportSortByOperatorWithDateExpressions() { + + carter.setDateOfBirth(new LocalDate(2000, 2, 1).toDate()); + dave.setDateOfBirth(new LocalDate(2000, 1, 1).toDate()); + oliver.setDateOfBirth(new LocalDate(2003, 5, 1).toDate()); + + List users = repository.findAll(QUser.user.id.goe(0), QUser.user.dateOfBirth.yearMonth().asc()); + + assertThat(users, hasSize(3)); + assertThat(users, hasItems(dave, carter, oliver)); + } }