From 37185b9bfa1a7794432a62ae5fd082a9f00f1167 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20PERALTA?= Date: Wed, 29 Mar 2017 11:53:20 +0200 Subject: [PATCH] DATAJPA-1080 - Use From instead of Root to sort Join --- .../data/jpa/repository/query/QueryUtils.java | 17 +++++++++-------- .../query/QueryUtilsIntegrationTests.java | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java b/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java index 6ae1452d0..177f08183 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java @@ -70,6 +70,7 @@ import org.springframework.util.StringUtils; * @author Komi Innocent * @author Christoph Strobl * @author Mark Paluch + * @author Sébastien Péralta */ public abstract class QueryUtils { @@ -478,11 +479,11 @@ public abstract class QueryUtils { * Turns the given {@link Sort} into {@link javax.persistence.criteria.Order}s. * * @param sort the {@link Sort} instance to be transformed into JPA {@link javax.persistence.criteria.Order}s. - * @param root must not be {@literal null}. + * @param from must not be {@literal null}. * @param cb must not be {@literal null}. * @return */ - public static List toOrders(Sort sort, Root root, CriteriaBuilder cb) { + public static List toOrders(Sort sort, From from, CriteriaBuilder cb) { List orders = new ArrayList(); @@ -490,11 +491,11 @@ public abstract class QueryUtils { return orders; } - Assert.notNull(root, "Root must not be null!"); + Assert.notNull(from, "From must not be null!"); Assert.notNull(cb, "CriteriaBuilder must not be null!"); for (org.springframework.data.domain.Sort.Order order : sort) { - orders.add(toJpaOrder(order, root, cb)); + orders.add(toJpaOrder(order, from, cb)); } return orders; @@ -533,15 +534,15 @@ public abstract class QueryUtils { * Creates a criteria API {@link javax.persistence.criteria.Order} from the given {@link Order}. * * @param order the order to transform into a JPA {@link javax.persistence.criteria.Order} - * @param root the {@link Root} the {@link Order} expression is based on + * @param from the {@link From} the {@link Order} expression is based on * @param cb the {@link CriteriaBuilder} to build the {@link javax.persistence.criteria.Order} with * @return */ @SuppressWarnings("unchecked") - private static javax.persistence.criteria.Order toJpaOrder(Order order, Root root, CriteriaBuilder cb) { + private static javax.persistence.criteria.Order toJpaOrder(Order order, From from, CriteriaBuilder cb) { - PropertyPath property = PropertyPath.from(order.getProperty(), root.getJavaType()); - Expression expression = toExpressionRecursively(root, property); + PropertyPath property = PropertyPath.from(order.getProperty(), from.getJavaType()); + Expression expression = toExpressionRecursively(from, property); if (order.isIgnoreCase() && String.class.equals(expression.getJavaType())) { Expression lower = cb.lower((Expression) expression); diff --git a/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsIntegrationTests.java b/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsIntegrationTests.java index f2aec0726..0a9bfe70f 100644 --- a/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsIntegrationTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsIntegrationTests.java @@ -33,6 +33,7 @@ import javax.persistence.Persistence; import javax.persistence.PersistenceContext; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Join; import javax.persistence.criteria.JoinType; import javax.persistence.criteria.Root; import javax.persistence.spi.PersistenceProvider; @@ -42,6 +43,8 @@ import javax.persistence.spi.PersistenceProviderResolverHolder; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; +import org.springframework.data.domain.Sort; +import org.springframework.data.domain.Sort.Direction; import org.springframework.data.jpa.domain.sample.Category; import org.springframework.data.jpa.domain.sample.Order; import org.springframework.data.jpa.domain.sample.User; @@ -54,6 +57,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; * Integration tests for {@link QueryUtils}. * * @author Oliver Gierke + * @author Sébastien Péralta */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:infrastructure.xml") @@ -151,6 +155,20 @@ public class QueryUtilsIntegrationTests { verify(mock, times(0)).join(Mockito.eq("product"), Mockito.any(JoinType.class)); } + @Test // DATAJPA-1080 + public void sortByJoinColumn() { + CriteriaBuilder builder = em.getCriteriaBuilder(); + CriteriaQuery query = builder.createQuery(User.class); + Root root = query.from(User.class); + Join join = root.join("manager", JoinType.LEFT); + + Sort sort = new Sort(Direction.ASC, "age"); + + List orders = QueryUtils.toOrders(sort, join, builder); + + assertThat(orders, hasSize(1)); + } + @Entity static class Merchant {