DATAJPA-1074 - Polishing.
Slightly changed the implementation to reject IsEmpty for non-collection properties. Minor formatting in unit tests. Original pull request: #190.
This commit is contained in:
@@ -310,11 +310,14 @@ public class JpaQueryCreator extends AbstractQueryCreator<CriteriaQuery<? extend
|
||||
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);
|
||||
|
||||
if (!property.getLeafProperty().isCollection()) {
|
||||
throw new IllegalArgumentException("IsEmpty / IsNotEmpty can only be used on collection properties!");
|
||||
}
|
||||
|
||||
Expression<Collection<Object>> collectionPath = traversePath(root, property);
|
||||
return type.equals(IS_NOT_EMPTY) ? builder.isNotEmpty(collectionPath) : builder.isEmpty(collectionPath);
|
||||
|
||||
default:
|
||||
throw new IllegalArgumentException("Unsupported keyword " + type);
|
||||
}
|
||||
|
||||
@@ -121,7 +121,7 @@ public class PartTreeJpaQueryIntegrationTests {
|
||||
JpaQueryMethod queryMethod = getQueryMethod("existsByFirstname", String.class);
|
||||
PartTreeJpaQuery jpaQuery = new PartTreeJpaQuery(queryMethod, entityManager, provider);
|
||||
|
||||
Query query = jpaQuery.createQuery(new Object[]{"Matthews"});
|
||||
Query query = jpaQuery.createQuery(new Object[] { "Matthews" });
|
||||
|
||||
assertThat(query.getMaxResults(), is(1));
|
||||
}
|
||||
@@ -132,7 +132,7 @@ public class PartTreeJpaQueryIntegrationTests {
|
||||
JpaQueryMethod queryMethod = getQueryMethod("existsByFirstname", String.class);
|
||||
PartTreeJpaQuery jpaQuery = new PartTreeJpaQuery(queryMethod, entityManager, provider);
|
||||
|
||||
Query query = jpaQuery.createQuery(new Object[]{"Matthews"});
|
||||
Query query = jpaQuery.createQuery(new Object[] { "Matthews" });
|
||||
|
||||
assertThat(HibernateUtils.getHibernateQuery(getValue(query, PROPERTY)), containsString(".id from User as"));
|
||||
}
|
||||
@@ -159,6 +159,15 @@ public class PartTreeJpaQueryIntegrationTests {
|
||||
assertThat(HibernateUtils.getHibernateQuery(getValue(query, PROPERTY)), endsWith("roles is not empty"));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class) // DATAJPA-1074
|
||||
public void rejectsIsEmptyOnNonCollectionProperty() throws Exception {
|
||||
|
||||
JpaQueryMethod method = getQueryMethod("findByFirstnameIsEmpty");
|
||||
AbstractJpaQuery jpaQuery = new PartTreeJpaQuery(method, entityManager, provider);
|
||||
|
||||
jpaQuery.createQuery(new Object[] { "Oliver" });
|
||||
}
|
||||
|
||||
private void testIgnoreCase(String methodName, Object... values) throws Exception {
|
||||
|
||||
Class<?>[] parameterTypes = new Class[values.length];
|
||||
@@ -219,5 +228,7 @@ public class PartTreeJpaQueryIntegrationTests {
|
||||
List<User> findByRolesIsEmpty();
|
||||
|
||||
List<User> findByRolesIsNotEmpty();
|
||||
|
||||
List<User> findByFirstnameIsEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user