DATAJPA-1074 - Added support for IsEmpty/IsNotEmpty keywords in query derivation.
We now translate the newly introduced IsEmpty / IsNotEmpty keywords into the corresponding Criteria API artifacts. Original pull request: #190.
This commit is contained in:
committed by
Oliver Gierke
parent
d69c2cb676
commit
90c8361679
@@ -49,6 +49,7 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
* @author Michael Cramer
|
||||
*/
|
||||
public class JpaQueryCreator extends AbstractQueryCreator<CriteriaQuery<? extends Object>, Predicate> {
|
||||
|
||||
@@ -307,6 +308,13 @@ public class JpaQueryCreator extends AbstractQueryCreator<CriteriaQuery<? extend
|
||||
case NEGATING_SIMPLE_PROPERTY:
|
||||
return builder.notEqual(upperIfIgnoreCase(getTypedPath(root, part)),
|
||||
upperIfIgnoreCase(provider.next(part).getExpression()));
|
||||
case IS_EMPTY:
|
||||
case IS_NOT_EMPTY:
|
||||
if (property.getLeafProperty().isCollection()) {
|
||||
Expression<Collection<Object>> emptyExpression = traversePath(root, property);
|
||||
return type.equals(IS_NOT_EMPTY) ? builder.isNotEmpty(emptyExpression)
|
||||
: builder.isEmpty(emptyExpression);
|
||||
}
|
||||
default:
|
||||
throw new IllegalArgumentException("Unsupported keyword " + type);
|
||||
}
|
||||
|
||||
@@ -57,6 +57,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
* @author Michael Cramer
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("classpath:infrastructure.xml")
|
||||
@@ -136,6 +137,28 @@ public class PartTreeJpaQueryIntegrationTests {
|
||||
assertThat(HibernateUtils.getHibernateQuery(getValue(query, PROPERTY)), containsString(".id from User as"));
|
||||
}
|
||||
|
||||
@Test // DATAJPA-1074
|
||||
public void isEmptyCollection() throws Exception {
|
||||
|
||||
JpaQueryMethod queryMethod = getQueryMethod("findByRolesIsEmpty");
|
||||
PartTreeJpaQuery jpaQuery = new PartTreeJpaQuery(queryMethod, entityManager, provider);
|
||||
|
||||
Query query = jpaQuery.createQuery(new Object[] {});
|
||||
|
||||
assertThat(HibernateUtils.getHibernateQuery(getValue(query, PROPERTY)), endsWith("roles is empty"));
|
||||
}
|
||||
|
||||
@Test // DATAJPA-1074
|
||||
public void isNotEmptyCollection() throws Exception {
|
||||
|
||||
JpaQueryMethod queryMethod = getQueryMethod("findByRolesIsNotEmpty");
|
||||
PartTreeJpaQuery jpaQuery = new PartTreeJpaQuery(queryMethod, entityManager, provider);
|
||||
|
||||
Query query = jpaQuery.createQuery(new Object[] {});
|
||||
|
||||
assertThat(HibernateUtils.getHibernateQuery(getValue(query, PROPERTY)), endsWith("roles is not empty"));
|
||||
}
|
||||
|
||||
private void testIgnoreCase(String methodName, Object... values) throws Exception {
|
||||
|
||||
Class<?>[] parameterTypes = new Class[values.length];
|
||||
@@ -192,5 +215,9 @@ public class PartTreeJpaQueryIntegrationTests {
|
||||
boolean existsByFirstname(String firstname);
|
||||
|
||||
List<User> findByCreatedAtAfter(@Temporal(TemporalType.TIMESTAMP) @Param("refDate") Date refDate);
|
||||
|
||||
List<User> findByRolesIsEmpty();
|
||||
|
||||
List<User> findByRolesIsNotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user