Skip to content

DATAJPA-1074 - IsEmpty and IsNotEmpty #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
*
* @author Oliver Gierke
* @author Mark Paluch
* @author Michael Cramer
*/
public class JpaQueryCreator extends AbstractQueryCreator<CriteriaQuery<? extends Object>, Predicate> {

Expand Down Expand Up @@ -306,6 +307,13 @@ public Predicate build() {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
*
* @author Oliver Gierke
* @author Mark Paluch
* @author Michael Cramer
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:infrastructure.xml")
Expand Down Expand Up @@ -99,6 +100,28 @@ public void cannotIgnoreCaseIfNotStringUnlessIgnoringAll() throws Exception {
testIgnoreCase("findByIdAllIgnoringCase", 3);
}

@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"));
}

@Test // DATAJPA-121
public void recreatesQueryIfNullValueIsGiven() throws Exception {

Expand Down Expand Up @@ -192,5 +215,9 @@ interface UserRepository extends Repository<User, Long> {
boolean existsByFirstname(String firstname);

List<User> findByCreatedAtAfter(@Temporal(TemporalType.TIMESTAMP) @Param("refDate") Date refDate);

List<User> findByRolesIsEmpty();

List<User> findByRolesIsNotEmpty();
}
}