Skip to content

Commit

Permalink
DATAJPA-1074 - Polishing.
Browse files Browse the repository at this point in the history
Slightly changed the implementation to reject IsEmpty for non-collection properties. Minor formatting in unit tests.

Original pull request: #190.
  • Loading branch information
odrotbohm committed Mar 13, 2017
1 parent 3412bcc commit e0d0386
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,14 @@ public Predicate build() {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void shouldLimitExistsProjectionQueries() throws Exception {
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));
}
Expand All @@ -132,7 +132,7 @@ public void shouldSelectAliasedIdForExistsProjectionQueries() throws Exception {
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"));
}
Expand All @@ -159,6 +159,15 @@ public void isNotEmptyCollection() throws Exception {
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];
Expand Down Expand Up @@ -219,5 +228,7 @@ interface UserRepository extends Repository<User, Long> {
List<User> findByRolesIsEmpty();

List<User> findByRolesIsNotEmpty();

List<User> findByFirstnameIsEmpty();
}
}

0 comments on commit e0d0386

Please sign in to comment.