Skip to content

Commit 9fd0e47

Browse files
schaudergregturn
authored andcommitted
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.
1 parent 65d524e commit 9fd0e47

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ static class ParameterMetadata<T> {
196196
private final ParameterExpression<T> expression;
197197
private final EscapeCharacter escape;
198198
private final boolean ignoreCase;
199+
private final boolean noWildcards;
199200

200201
/**
201202
* Creates a new {@link ParameterMetadata}.
@@ -206,6 +207,7 @@ public ParameterMetadata(ParameterExpression<T> expression, Part part, @Nullable
206207
this.expression = expression;
207208
this.type = value == null && Type.SIMPLE_PROPERTY.equals(part.getType()) ? Type.IS_NULL : part.getType();
208209
this.ignoreCase = IgnoreCaseType.ALWAYS.equals(part.shouldIgnoreCase());
210+
this.noWildcards = part.getProperty().getLeafProperty().isCollection();
209211
this.escape = escape;
210212
}
211213

@@ -241,7 +243,7 @@ public Object prepare(Object value) {
241243
return value;
242244
}
243245

244-
if (String.class.equals(expressionType)) {
246+
if (String.class.equals(expressionType) && !noWildcards) {
245247

246248
switch (type) {
247249
case STARTING_WITH:

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2968,6 +2968,24 @@ void complexWithNativeStatement() {
29682968
assertThat(foundData).containsExactly("joachim", "dave", "kevin");
29692969
}
29702970

2971+
@Test // GH-2607
2972+
void containsWithCollection(){
2973+
2974+
firstUser.getAttributes().add("cool");
2975+
firstUser.getAttributes().add("hip");
2976+
2977+
secondUser.getAttributes().add("hip");
2978+
2979+
thirdUser.getAttributes().add("rockstar");
2980+
thirdUser.getAttributes().add("%hip%");
2981+
2982+
flushTestUsers();
2983+
2984+
List<User> result = repository.findByAttributesContains("hip");
2985+
2986+
assertThat(result).containsOnly(firstUser, secondUser);
2987+
}
2988+
29712989
private Page<User> executeSpecWithSort(Sort sort) {
29722990

29732991
flushTestUsers();

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,9 @@ List<String> findAllAndSortByFunctionResultNamedParameter(@Param("namedParameter
676676
nativeQuery = true)
677677
List<String> complexWithNativeStatement();
678678

679+
// GH-2607
680+
List<User> findByAttributesContains(String attribute);
681+
679682
interface RolesAndFirstname {
680683

681684
String getFirstname();

0 commit comments

Comments
 (0)