Skip to content

Commit 9fc57ea

Browse files
Thomas Darimontodrotbohm
Thomas Darimont
authored andcommitted
DATACMNS-566 - Allow sorting by QueryDsl operator expressions.
Previously we only allowed to sort by QueryDsl path expressions. With this change we now also support ordering by operator expressions, e.g. yearMonth() on a date property. Original pull request: #94.
1 parent edfd341 commit 9fc57ea

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

src/main/java/org/springframework/data/querydsl/QSort.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-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.
@@ -25,6 +25,7 @@
2525

2626
import com.mysema.query.types.Expression;
2727
import com.mysema.query.types.OrderSpecifier;
28+
import com.mysema.query.types.Path;
2829

2930
/**
3031
* Sort option for queries that wraps a querydsl {@link OrderSpecifier}.
@@ -86,7 +87,8 @@ private static Order toOrder(OrderSpecifier<?> orderSpecifier) {
8687
Assert.notNull(orderSpecifier, "Order specifier must not be null!");
8788

8889
Expression<?> target = orderSpecifier.getTarget();
89-
Object targetElement = ((com.mysema.query.types.Path<?>) target).getMetadata().getElement();
90+
Object targetElement = target instanceof Path ? ((com.mysema.query.types.Path<?>) target).getMetadata()
91+
.getElement() : target;
9092

9193
Assert.notNull(targetElement, "Target element must not be null!");
9294

src/test/java/org/springframework/data/querydsl/QSortUnitTests.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-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.
@@ -15,8 +15,9 @@
1515
*/
1616
package org.springframework.data.querydsl;
1717

18-
import static org.hamcrest.Matchers.*;
19-
import static org.junit.Assert.*;
18+
import static org.hamcrest.Matchers.hasItems;
19+
import static org.hamcrest.Matchers.is;
20+
import static org.junit.Assert.assertThat;
2021

2122
import java.util.List;
2223

@@ -146,4 +147,18 @@ public void concatenatesPlainSortCorrectly() {
146147
assertThat(result, is(Matchers.<Order> iterableWithSize(2)));
147148
assertThat(result, hasItems(new Order(Direction.ASC, "lastname"), new Order(Direction.ASC, "firstname")));
148149
}
150+
151+
/**
152+
* @see DATACMNS-566
153+
*/
154+
@Test
155+
public void shouldSupportSortByOperatorExpressions() {
156+
157+
QUser user = QUser.user;
158+
QSort sort = new QSort(user.dateOfBirth.yearMonth().asc());
159+
160+
Sort result = sort.and(new Sort(Direction.ASC, "lastname"));
161+
assertThat(result, is(Matchers.<Order> iterableWithSize(2)));
162+
assertThat(result, hasItems(new Order(Direction.ASC, "lastname"), new Order(Direction.ASC, user.dateOfBirth.yearMonth().toString())));
163+
}
149164
}

src/test/java/org/springframework/data/querydsl/User.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2013 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.
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.data.querydsl;
1717

18+
import java.util.Date;
19+
1820
import com.mysema.query.annotations.QueryEntity;
1921

2022
/**
@@ -25,4 +27,5 @@
2527
public class User {
2628
String firstname;
2729
String lastname;
30+
Date dateOfBirth;
2831
}

0 commit comments

Comments
 (0)