Skip to content

Commit e0d0386

Browse files
committed
DATAJPA-1074 - Polishing.
Slightly changed the implementation to reject IsEmpty for non-collection properties. Minor formatting in unit tests. Original pull request: #190.
1 parent 3412bcc commit e0d0386

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/main/java/org/springframework/data/jpa/repository/query/JpaQueryCreator.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,14 @@ public Predicate build() {
309309
upperIfIgnoreCase(provider.next(part).getExpression()));
310310
case IS_EMPTY:
311311
case IS_NOT_EMPTY:
312-
if (property.getLeafProperty().isCollection()) {
313-
Expression<Collection<Object>> emptyExpression = traversePath(root, property);
314-
return type.equals(IS_NOT_EMPTY) ? builder.isNotEmpty(emptyExpression)
315-
: builder.isEmpty(emptyExpression);
312+
313+
if (!property.getLeafProperty().isCollection()) {
314+
throw new IllegalArgumentException("IsEmpty / IsNotEmpty can only be used on collection properties!");
316315
}
316+
317+
Expression<Collection<Object>> collectionPath = traversePath(root, property);
318+
return type.equals(IS_NOT_EMPTY) ? builder.isNotEmpty(collectionPath) : builder.isEmpty(collectionPath);
319+
317320
default:
318321
throw new IllegalArgumentException("Unsupported keyword " + type);
319322
}

src/test/java/org/springframework/data/jpa/repository/query/PartTreeJpaQueryIntegrationTests.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void shouldLimitExistsProjectionQueries() throws Exception {
121121
JpaQueryMethod queryMethod = getQueryMethod("existsByFirstname", String.class);
122122
PartTreeJpaQuery jpaQuery = new PartTreeJpaQuery(queryMethod, entityManager, provider);
123123

124-
Query query = jpaQuery.createQuery(new Object[]{"Matthews"});
124+
Query query = jpaQuery.createQuery(new Object[] { "Matthews" });
125125

126126
assertThat(query.getMaxResults(), is(1));
127127
}
@@ -132,7 +132,7 @@ public void shouldSelectAliasedIdForExistsProjectionQueries() throws Exception {
132132
JpaQueryMethod queryMethod = getQueryMethod("existsByFirstname", String.class);
133133
PartTreeJpaQuery jpaQuery = new PartTreeJpaQuery(queryMethod, entityManager, provider);
134134

135-
Query query = jpaQuery.createQuery(new Object[]{"Matthews"});
135+
Query query = jpaQuery.createQuery(new Object[] { "Matthews" });
136136

137137
assertThat(HibernateUtils.getHibernateQuery(getValue(query, PROPERTY)), containsString(".id from User as"));
138138
}
@@ -159,6 +159,15 @@ public void isNotEmptyCollection() throws Exception {
159159
assertThat(HibernateUtils.getHibernateQuery(getValue(query, PROPERTY)), endsWith("roles is not empty"));
160160
}
161161

162+
@Test(expected = IllegalArgumentException.class) // DATAJPA-1074
163+
public void rejectsIsEmptyOnNonCollectionProperty() throws Exception {
164+
165+
JpaQueryMethod method = getQueryMethod("findByFirstnameIsEmpty");
166+
AbstractJpaQuery jpaQuery = new PartTreeJpaQuery(method, entityManager, provider);
167+
168+
jpaQuery.createQuery(new Object[] { "Oliver" });
169+
}
170+
162171
private void testIgnoreCase(String methodName, Object... values) throws Exception {
163172

164173
Class<?>[] parameterTypes = new Class[values.length];
@@ -219,5 +228,7 @@ interface UserRepository extends Repository<User, Long> {
219228
List<User> findByRolesIsEmpty();
220229

221230
List<User> findByRolesIsNotEmpty();
231+
232+
List<User> findByFirstnameIsEmpty();
222233
}
223234
}

0 commit comments

Comments
 (0)