DATAJPA-830 - NotContaining now gets applied correctly.

We now handle the NotContaining keyword correctly for String property expressions.
This commit is contained in:
Oliver Gierke
2015-12-02 07:40:54 +01:00
parent 705666b492
commit 033b4bfd18
4 changed files with 22 additions and 5 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.data.jpa.repository.query;
import static org.springframework.data.jpa.repository.query.QueryUtils.*;
import static org.springframework.data.repository.query.parser.Part.Type.*;
import java.util.Collection;
import java.util.Iterator;
@@ -197,8 +198,9 @@ public class JpaQueryCreator extends AbstractQueryCreator<CriteriaQuery<Object>,
PropertyPath property = part.getProperty();
Expression<Object> path = toExpressionRecursively(root, property);
Type type = part.getType();
switch (part.getType()) {
switch (type) {
case BETWEEN:
ParameterMetadata<Comparable> first = provider.next(part);
ParameterMetadata<Comparable> second = provider.next(part);
@@ -229,11 +231,12 @@ public class JpaQueryCreator extends AbstractQueryCreator<CriteriaQuery<Object>,
case CONTAINING:
case LIKE:
case NOT_LIKE:
case NOT_CONTAINING:
Expression<String> stringPath = getTypedPath(root, part);
Expression<String> propertyExpression = upperIfIgnoreCase(stringPath);
Expression<String> parameterExpression = upperIfIgnoreCase(provider.next(part, String.class).getExpression());
Predicate like = builder.like(propertyExpression, parameterExpression);
return part.getType() == Type.NOT_LIKE ? like.not() : like;
return type.equals(NOT_LIKE) || type.equals(NOT_CONTAINING) ? like.not() : like;
case TRUE:
Expression<Boolean> truePath = getTypedPath(root, part);
return builder.isTrue(truePath);
@@ -247,7 +250,7 @@ public class JpaQueryCreator extends AbstractQueryCreator<CriteriaQuery<Object>,
case NEGATING_SIMPLE_PROPERTY:
return builder.notEqual(upperIfIgnoreCase(path), upperIfIgnoreCase(provider.next(part).getExpression()));
default:
throw new IllegalArgumentException("Unsupported keyword " + part.getType());
throw new IllegalArgumentException("Unsupported keyword " + type);
}
}

View File

@@ -234,6 +234,7 @@ class ParameterMetadataProvider {
case ENDING_WITH:
return String.format("%%%s", value.toString());
case CONTAINING:
case NOT_CONTAINING:
return String.format("%%%s%%", value.toString());
default:
return value;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2014 the original author or authors.
* Copyright 2008-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,7 +15,7 @@
*/
package org.springframework.data.jpa.repository;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.springframework.data.domain.Sort.Direction.*;
@@ -194,4 +194,12 @@ public class UserRepositoryFinderTests {
assertThat(slice.getContent(), hasItem(dave));
assertThat(slice.hasNext(), is(true));
}
/**
* @see DATAJPA-830
*/
@Test
public void executesMethodWithNotContainingOnStringCorrectly() {
assertThat(userRepository.findByLastnameNotContaining("u"), containsInAnyOrder(dave, oliver));
}
}

View File

@@ -575,4 +575,9 @@ public interface UserRepository
*/
@Query("select u from User u")
Stream<User> streamAllPaged(Pageable pageable);
/**
* @see DATAJPA-830
*/
List<User> findByLastnameNotContaining(String part);
}