From b6767afadc5dc74d140500534e9cac6db1ee5b70 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 20 Jan 2011 01:48:09 +0100 Subject: [PATCH] Fixed missing application of Pageable handed to query methods using query derivation from the method name. --- .../repository/query/PartTreeJpaQuery.java | 19 ++++++++++++++++--- .../repository/UserRepositoryFinderTests.java | 7 +++---- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/springframework/data/jpa/repository/query/PartTreeJpaQuery.java b/src/main/java/org/springframework/data/jpa/repository/query/PartTreeJpaQuery.java index dcb095ce1..99947a945 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/PartTreeJpaQuery.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/PartTreeJpaQuery.java @@ -17,8 +17,10 @@ package org.springframework.data.jpa.repository.query; import javax.persistence.EntityManager; import javax.persistence.Query; +import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaQuery; +import org.springframework.data.domain.Pageable; import org.springframework.data.repository.query.SimpleParameterAccessor; import org.springframework.data.repository.query.parser.PartTree; @@ -59,12 +61,23 @@ public class PartTreeJpaQuery extends AbstractJpaQuery { */ public Query createQuery(Object[] parameters) { + SimpleParameterAccessor accessor = + new SimpleParameterAccessor(getParameters(), parameters); + JpaQueryCreator jpaQueryCreator = - new JpaQueryCreator(tree, new SimpleParameterAccessor( - getParameters(), parameters), domainClass, + new JpaQueryCreator(tree, accessor, domainClass, getEntityManager()); - return getEntityManager().createQuery(jpaQueryCreator.createQuery()); + TypedQuery query = + getEntityManager().createQuery(jpaQueryCreator.createQuery()); + + if (getParameters().hasPageableParameter()) { + Pageable pageable = accessor.getPageable(); + query.setFirstResult(pageable.getOffset()); + query.setMaxResults(pageable.getPageSize()); + } + + return query; } diff --git a/src/test/java/org/springframework/data/jpa/repository/UserRepositoryFinderTests.java b/src/test/java/org/springframework/data/jpa/repository/UserRepositoryFinderTests.java index faff91eea..57d3749cd 100644 --- a/src/test/java/org/springframework/data/jpa/repository/UserRepositoryFinderTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/UserRepositoryFinderTests.java @@ -67,6 +67,7 @@ public class UserRepositoryFinderTests { secondUser = new User(); secondUser.setEmailAddress("bar"); secondUser.setLastname("foo"); + secondUser.setFirstname("foobar"); userRepository.save(secondUser); } @@ -117,8 +118,7 @@ public class UserRepositoryFinderTests { public void executesPagingMethodToPageCorrectly() throws Exception { Page page = - userRepository - .findByFirstname(new PageRequest(0, 20), "foobar"); + userRepository.findByFirstname(new PageRequest(0, 1), "foobar"); assertEquals(1, page.getNumberOfElements()); } @@ -127,8 +127,7 @@ public class UserRepositoryFinderTests { public void executesPagingMethodToListCorrectly() throws Exception { List list = - userRepository - .findByFirstname("foobar", new PageRequest(0, 20)); + userRepository.findByFirstname("foobar", new PageRequest(0, 1)); assertThat(list.size(), is(1)); } }