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.
This commit is contained in:
committed by
Oliver Gierke
parent
edfd341d64
commit
9fc57ea3bf
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013 the original author or authors.
|
||||
* Copyright 2013-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.
|
||||
@@ -25,6 +25,7 @@ import org.springframework.util.Assert;
|
||||
|
||||
import com.mysema.query.types.Expression;
|
||||
import com.mysema.query.types.OrderSpecifier;
|
||||
import com.mysema.query.types.Path;
|
||||
|
||||
/**
|
||||
* Sort option for queries that wraps a querydsl {@link OrderSpecifier}.
|
||||
@@ -86,7 +87,8 @@ public class QSort extends Sort implements Serializable {
|
||||
Assert.notNull(orderSpecifier, "Order specifier must not be null!");
|
||||
|
||||
Expression<?> target = orderSpecifier.getTarget();
|
||||
Object targetElement = ((com.mysema.query.types.Path<?>) target).getMetadata().getElement();
|
||||
Object targetElement = target instanceof Path ? ((com.mysema.query.types.Path<?>) target).getMetadata()
|
||||
.getElement() : target;
|
||||
|
||||
Assert.notNull(targetElement, "Target element must not be null!");
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013 the original author or authors.
|
||||
* Copyright 2013-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.
|
||||
@@ -15,8 +15,9 @@
|
||||
*/
|
||||
package org.springframework.data.querydsl;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.hamcrest.Matchers.hasItems;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -146,4 +147,18 @@ public class QSortUnitTests {
|
||||
assertThat(result, is(Matchers.<Order> iterableWithSize(2)));
|
||||
assertThat(result, hasItems(new Order(Direction.ASC, "lastname"), new Order(Direction.ASC, "firstname")));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-566
|
||||
*/
|
||||
@Test
|
||||
public void shouldSupportSortByOperatorExpressions() {
|
||||
|
||||
QUser user = QUser.user;
|
||||
QSort sort = new QSort(user.dateOfBirth.yearMonth().asc());
|
||||
|
||||
Sort result = sort.and(new Sort(Direction.ASC, "lastname"));
|
||||
assertThat(result, is(Matchers.<Order> iterableWithSize(2)));
|
||||
assertThat(result, hasItems(new Order(Direction.ASC, "lastname"), new Order(Direction.ASC, user.dateOfBirth.yearMonth().toString())));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2013 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.
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.data.querydsl;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import com.mysema.query.annotations.QueryEntity;
|
||||
|
||||
/**
|
||||
@@ -25,4 +27,5 @@ import com.mysema.query.annotations.QueryEntity;
|
||||
public class User {
|
||||
String firstname;
|
||||
String lastname;
|
||||
Date dateOfBirth;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user