diff --git a/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryCreator.java b/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryCreator.java index d945948e3e..71d20f79c6 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryCreator.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryCreator.java @@ -309,11 +309,14 @@ public Predicate build() { upperIfIgnoreCase(provider.next(part).getExpression())); case IS_EMPTY: case IS_NOT_EMPTY: - if (property.getLeafProperty().isCollection()) { - Expression> 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> collectionPath = traversePath(root, property); + return type.equals(IS_NOT_EMPTY) ? builder.isNotEmpty(collectionPath) : builder.isEmpty(collectionPath); + default: throw new IllegalArgumentException("Unsupported keyword " + type); } diff --git a/src/test/java/org/springframework/data/jpa/repository/query/PartTreeJpaQueryIntegrationTests.java b/src/test/java/org/springframework/data/jpa/repository/query/PartTreeJpaQueryIntegrationTests.java index 2af8c95ec6..4215f105a2 100644 --- a/src/test/java/org/springframework/data/jpa/repository/query/PartTreeJpaQueryIntegrationTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/query/PartTreeJpaQueryIntegrationTests.java @@ -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)); } @@ -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")); } @@ -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]; @@ -219,5 +228,7 @@ interface UserRepository extends Repository { List findByRolesIsEmpty(); List findByRolesIsNotEmpty(); + + List findByFirstnameIsEmpty(); } }