Skip to content

Commit

Permalink
DATAJPA-29, DATAJPA-30 - Fixed invalid page metadata in pagination qu…
Browse files Browse the repository at this point in the history
…eries.

JpaCountQueryCreator now correctly applies the predicate. Added test cases and support for 'In' keywords.
  • Loading branch information
odrotbohm committed Feb 22, 2011
1 parent ca21bfe commit 14a69ea
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ public JpaCountQueryCreator(PartTree tree, ParameterAccessor parameters,
protected CriteriaQuery<Object> complete(Predicate predicate, Sort sort,
CriteriaQuery<Object> query, CriteriaBuilder builder, Root<?> root) {

return query.select(builder.count(root));
return query.select(builder.count(root)).where(predicate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.springframework.data.jpa.repository.query;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

import javax.persistence.EntityManager;
Expand All @@ -34,6 +36,7 @@
import org.springframework.data.repository.query.parser.PartTree;
import org.springframework.data.repository.query.parser.Property;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;


/**
Expand Down Expand Up @@ -175,6 +178,8 @@ root.<Comparable> get(part.getProperty().toDotPath()),
return path.isNull();
case IS_NOT_NULL:
return path.isNotNull();
case IN:
return path.in(nextAsCollection(iterator));
case LIKE:
return builder.like(root.<String> get(part.getProperty()
.toDotPath()), iterator.next().toString());
Expand Down Expand Up @@ -248,4 +253,18 @@ private Comparable nextAsComparable(Iterator<Object> iterator) {

return (Comparable<?>) next;
}


private Collection<?> nextAsCollection(Iterator<Object> iterator) {

Object next = iterator.next();

if (next instanceof Collection) {
return (Collection<?>) next;
} else if (next.getClass().isArray()) {
return CollectionUtils.arrayToList(next);
}

return Arrays.asList(next);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public Sort getSort() {
public Query bind(Query query) {

int methodParameterPosition = 0;
int queryParameterPosition = 1;

for (Parameter parameter : parameters) {

Expand All @@ -118,7 +119,7 @@ public Query bind(Query query) {
if (hasNamedParameter(query) && parameter.isNamedParameter()) {
query.setParameter(parameter.getName(), value);
} else {
query.setParameter(parameter.getIndex() + 1, value);
query.setParameter(queryParameterPosition++, value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,22 @@ public class UserRepositoryFinderTests {
@Autowired
UserRepository userRepository;

User firstUser, secondUser;
User dave, carter, oliver;


@Before
public void setUp() {

// This one matches both criterias
firstUser = new User();
firstUser.setEmailAddress("foo");
firstUser.setLastname("bar");
firstUser.setFirstname("foobar");

userRepository.save(firstUser);
dave = new User("Dave", "Matthews", "dave@dmband.com");
userRepository.save(dave);

// This one matches only the second one
secondUser = new User();
secondUser.setEmailAddress("bar");
secondUser.setLastname("foo");
secondUser.setFirstname("foobar");
carter = new User("Carter", "Beauford", "carter@dmband.com");
userRepository.save(carter);

userRepository.save(secondUser);
oliver = new User("Oliver August", "Matthews", "oliver@dmband.com");
userRepository.save(oliver);
}


Expand All @@ -79,8 +74,10 @@ public void setUp() {
@Test
public void testSimpleCustomCreatedFinder() {

User user = userRepository.findByEmailAddressAndLastname("foo", "bar");
assertEquals(firstUser, user);
User user =
userRepository.findByEmailAddressAndLastname("dave@dmband.com",
"Matthews");
assertEquals(dave, user);
}


Expand All @@ -104,21 +101,22 @@ public void returnsNullIfNothingFound() {
public void testAndOrFinder() {

List<User> users =
userRepository.findByEmailAddressAndLastnameOrFirstname("bar",
"foo", "foobar");
userRepository.findByEmailAddressAndLastnameOrFirstname(
"dave@dmband.com", "Matthews", "Carter");

assertNotNull(users);
assertEquals(2, users.size());
assertTrue(users.contains(firstUser));
assertTrue(users.contains(secondUser));
assertTrue(users.contains(dave));
assertTrue(users.contains(carter));
}


@Test
public void executesPagingMethodToPageCorrectly() throws Exception {

Page<User> page =
userRepository.findByFirstname(new PageRequest(0, 1), "foobar");
userRepository
.findByLastname(new PageRequest(0, 1), "Matthews");
assertThat(page.getNumberOfElements(), is(1));
assertThat(page.getTotalElements(), is(2L));
assertThat(page.getTotalPages(), is(2));
Expand All @@ -129,7 +127,20 @@ public void executesPagingMethodToPageCorrectly() throws Exception {
public void executesPagingMethodToListCorrectly() throws Exception {

List<User> list =
userRepository.findByFirstname("foobar", new PageRequest(0, 1));
userRepository.findByFirstname("Carter", new PageRequest(0, 1));
assertThat(list.size(), is(1));
}


@Test
public void testname() throws Exception {

Page<User> page =
userRepository.findByFirstnameIn(new PageRequest(0, 1), "Dave",
"Oliver August");

assertThat(page.getNumberOfElements(), is(1));
assertThat(page.getTotalElements(), is(2L));
assertThat(page.getTotalPages(), is(2));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public void rejectsPageablesOnPersistenceProvidersNotExtractingQueries()
throws Exception {

Method method =
UserRepository.class.getMethod("findByFirstname",
UserRepository.class.getMethod("findByLastname",
Pageable.class, String.class);

when(extractor.canExtractQuery()).thenReturn(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ List<User> findByEmailAddressAndLastnameOrFirstname(String emailAddress,
* parameter to be regarded on query execution.
*
* @param pageable
* @param firstname
* @param lastname
* @return
*/
Page<User> findByFirstname(Pageable pageable, String firstname);
Page<User> findByLastname(Pageable pageable, String lastname);


/**
Expand All @@ -133,6 +133,9 @@ List<User> findByEmailAddressAndLastnameOrFirstname(String emailAddress,
List<User> findByFirstname(String firstname, Pageable pageable);


Page<User> findByFirstnameIn(Pageable pageable, String... firstnames);


/**
* Manipulating query to set all {@link User}'s names to the given one.
*
Expand Down

0 comments on commit 14a69ea

Please sign in to comment.