Skip to content

Commit e0a9f28

Browse files
committed
DATAJPA-830 - NotContaining now gets applied correctly.
We now handle the NotContaining keyword correctly for String property expressions.
1 parent a3b6ca6 commit e0a9f28

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.jpa.repository.query;
1717

1818
import static org.springframework.data.jpa.repository.query.QueryUtils.*;
19+
import static org.springframework.data.repository.query.parser.Part.Type.*;
1920

2021
import java.util.Collection;
2122
import java.util.Iterator;
@@ -197,8 +198,9 @@ public Predicate build() {
197198

198199
PropertyPath property = part.getProperty();
199200
Expression<Object> path = toExpressionRecursively(root, property);
201+
Type type = part.getType();
200202

201-
switch (part.getType()) {
203+
switch (type) {
202204
case BETWEEN:
203205
ParameterMetadata<Comparable> first = provider.next(part);
204206
ParameterMetadata<Comparable> second = provider.next(part);
@@ -229,11 +231,12 @@ public Predicate build() {
229231
case CONTAINING:
230232
case LIKE:
231233
case NOT_LIKE:
234+
case NOT_CONTAINING:
232235
Expression<String> stringPath = getTypedPath(root, part);
233236
Expression<String> propertyExpression = upperIfIgnoreCase(stringPath);
234237
Expression<String> parameterExpression = upperIfIgnoreCase(provider.next(part, String.class).getExpression());
235238
Predicate like = builder.like(propertyExpression, parameterExpression);
236-
return part.getType() == Type.NOT_LIKE ? like.not() : like;
239+
return type.equals(NOT_LIKE) || type.equals(NOT_CONTAINING) ? like.not() : like;
237240
case TRUE:
238241
Expression<Boolean> truePath = getTypedPath(root, part);
239242
return builder.isTrue(truePath);
@@ -247,7 +250,7 @@ public Predicate build() {
247250
case NEGATING_SIMPLE_PROPERTY:
248251
return builder.notEqual(upperIfIgnoreCase(path), upperIfIgnoreCase(provider.next(part).getExpression()));
249252
default:
250-
throw new IllegalArgumentException("Unsupported keyword " + part.getType());
253+
throw new IllegalArgumentException("Unsupported keyword " + type);
251254
}
252255
}
253256

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ public Object prepare(Object value) {
234234
case ENDING_WITH:
235235
return String.format("%%%s", value.toString());
236236
case CONTAINING:
237+
case NOT_CONTAINING:
237238
return String.format("%%%s%%", value.toString());
238239
default:
239240
return value;

src/test/java/org/springframework/data/jpa/repository/UserRepositoryFinderTests.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2014 the original author or authors.
2+
* Copyright 2008-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.data.jpa.repository;
1717

18-
import static org.hamcrest.CoreMatchers.*;
18+
import static org.hamcrest.Matchers.*;
1919
import static org.junit.Assert.*;
2020
import static org.springframework.data.domain.Sort.Direction.*;
2121

@@ -194,4 +194,12 @@ public void executesQueryToSlice() {
194194
assertThat(slice.getContent(), hasItem(dave));
195195
assertThat(slice.hasNext(), is(true));
196196
}
197+
198+
/**
199+
* @see DATAJPA-830
200+
*/
201+
@Test
202+
public void executesMethodWithNotContainingOnStringCorrectly() {
203+
assertThat(userRepository.findByLastnameNotContaining("u"), containsInAnyOrder(dave, oliver));
204+
}
197205
}

src/test/java/org/springframework/data/jpa/repository/sample/UserRepository.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,4 +575,9 @@ List<User> findUsersByFirstnameForSpELExpressionWithParameterIndexOnlyWithEntity
575575
*/
576576
@Query("select u from User u")
577577
Stream<User> streamAllPaged(Pageable pageable);
578+
579+
/**
580+
* @see DATAJPA-830
581+
*/
582+
List<User> findByLastnameNotContaining(String part);
578583
}

0 commit comments

Comments
 (0)