DATAJPA-405 - Guard against null predicates on query creation.

We now only call CriteriaQuery.where(…) if the predicate we handle is a non-null value. Adapted Jpa(Count)QueryCreator accordingly.
This commit is contained in:
Oliver Gierke
2013-10-27 17:24:19 +01:00
parent 0a449d27a3
commit df55a8525d
5 changed files with 29 additions and 6 deletions

View File

@@ -27,7 +27,7 @@
<hsqldb1>1.8.0.10</hsqldb1>
<jpa>2.0.0</jpa>
<openjpa>2.2.1</openjpa>
<springdata.commons>1.6.2.RELEASE</springdata.commons>
<springdata.commons>1.6.3.BUILD-SNAPSHOT</springdata.commons>
</properties>
@@ -416,8 +416,8 @@
<repositories>
<repository>
<id>spring-libs-release</id>
<url>http://repo.springsource.org/libs-release-local</url>
<id>spring-libs-snapshot</id>
<url>http://repo.springsource.org/libs-snapshot-local</url>
</repository>
</repositories>

View File

@@ -50,6 +50,8 @@ public class JpaCountQueryCreator extends JpaQueryCreator {
@Override
protected CriteriaQuery<Object> complete(Predicate predicate, Sort sort, CriteriaQuery<Object> query,
CriteriaBuilder builder, Root<?> root) {
return query.select(builder.count(root)).where(predicate);
CriteriaQuery<Object> select = query.select(builder.count(root));
return predicate == null ? select : select.where(predicate);
}
}

View File

@@ -129,7 +129,9 @@ public class JpaQueryCreator extends AbstractQueryCreator<CriteriaQuery<Object>,
*/
protected CriteriaQuery<Object> complete(Predicate predicate, Sort sort, CriteriaQuery<Object> query,
CriteriaBuilder builder, Root<?> root) {
return this.query.select(root).where(predicate).orderBy(QueryUtils.toOrders(sort, root, builder));
CriteriaQuery<Object> select = this.query.select(root).orderBy(QueryUtils.toOrders(sort, root, builder));
return predicate == null ? select : select.where(predicate);
}
/**

View File

@@ -1093,6 +1093,20 @@ public class UserRepositoryTests {
assertThat(lastname, hasItem("Dave"));
}
/**
* @see DATAJPA-405
*/
@Test
public void executesFinderWithOrderClauseOnly() {
flushTestUsers();
List<User> result = repository.findAllByOrderByLastnameAsc();
assertThat(result, hasSize(4));
assertThat(result, contains(secondUser, firstUser, thirdUser, fourthUser));
}
private Page<User> executeSpecWithSort(Sort sort) {
flushTestUsers();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2012 the original author or authors.
* Copyright 2008-2013 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.
@@ -270,4 +270,9 @@ public interface UserRepository extends JpaRepository<User, Integer>, JpaSpecifi
*/
@Query("select u.firstname from User u where u.lastname = ?1")
List<String> findFirstnamesByLastname(String lastname);
/**
* @see DATAJPA-405
*/
List<User> findAllByOrderByLastnameAsc();
}