Support Contains with ElementCollection of type String.

Properly handle `contains` for an ElementCollection of type String in a LIKE query, wrapping the the parameter in wildcards only when needed.

Closes: #2607.
This commit is contained in:
Jens Schauder
2022-08-01 14:37:03 +02:00
committed by Greg L. Turnquist
parent 65d524e319
commit 9fd0e47514
3 changed files with 24 additions and 1 deletions

View File

@@ -196,6 +196,7 @@ class ParameterMetadataProvider {
private final ParameterExpression<T> expression;
private final EscapeCharacter escape;
private final boolean ignoreCase;
private final boolean noWildcards;
/**
* Creates a new {@link ParameterMetadata}.
@@ -206,6 +207,7 @@ class ParameterMetadataProvider {
this.expression = expression;
this.type = value == null && Type.SIMPLE_PROPERTY.equals(part.getType()) ? Type.IS_NULL : part.getType();
this.ignoreCase = IgnoreCaseType.ALWAYS.equals(part.shouldIgnoreCase());
this.noWildcards = part.getProperty().getLeafProperty().isCollection();
this.escape = escape;
}
@@ -241,7 +243,7 @@ class ParameterMetadataProvider {
return value;
}
if (String.class.equals(expressionType)) {
if (String.class.equals(expressionType) && !noWildcards) {
switch (type) {
case STARTING_WITH:

View File

@@ -2968,6 +2968,24 @@ public class UserRepositoryTests {
assertThat(foundData).containsExactly("joachim", "dave", "kevin");
}
@Test // GH-2607
void containsWithCollection(){
firstUser.getAttributes().add("cool");
firstUser.getAttributes().add("hip");
secondUser.getAttributes().add("hip");
thirdUser.getAttributes().add("rockstar");
thirdUser.getAttributes().add("%hip%");
flushTestUsers();
List<User> result = repository.findByAttributesContains("hip");
assertThat(result).containsOnly(firstUser, secondUser);
}
private Page<User> executeSpecWithSort(Sort sort) {
flushTestUsers();

View File

@@ -676,6 +676,9 @@ public interface UserRepository
nativeQuery = true)
List<String> complexWithNativeStatement();
// GH-2607
List<User> findByAttributesContains(String attribute);
interface RolesAndFirstname {
String getFirstname();