Skip to content

Commit a1054b6

Browse files
committed
Properly handle null values inside queries using LIKE or CONTAINS.
Null values are wrapped with a special handler when interacting with Hibernate. However, this becomes an issue for queries when LIKE or CONTAINS are applied. In this situation, the null needs to be condensed into an empty string and any wildcards can then be applied with expected results. Closes #2548, #2570. Supercedes: #2585. Related: #2461, #2544#
1 parent 44638e1 commit a1054b6

File tree

8 files changed

+378
-15
lines changed

8 files changed

+378
-15
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/provider/PersistenceProvider.java

+30
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.springframework.lang.Nullable;
4343
import org.springframework.transaction.support.TransactionSynchronizationManager;
4444
import org.springframework.util.Assert;
45+
import org.springframework.util.ClassUtils;
4546
import org.springframework.util.ConcurrentReferenceHashMap;
4647

4748
/**
@@ -114,6 +115,7 @@ public JpaParametersParameterAccessor getParameterAccessor(JpaParameters paramet
114115
public String getCommentHintKey() {
115116
return "org.hibernate.comment";
116117
}
118+
117119
},
118120

119121
/**
@@ -310,6 +312,34 @@ public boolean canExtractQuery() {
310312
}
311313
}
312314

315+
/**
316+
* Because {@linke TypedParameterValue} is only used to wrap a {@literal null}, swap it out with an empty string.
317+
*
318+
* @param value
319+
* @return the original value or an empty string.
320+
* @since 3.0
321+
*/
322+
public static Object condense(Object value) {
323+
324+
ClassLoader classLoader = PersistenceProvider.class.getClassLoader();
325+
326+
if (ClassUtils.isPresent("org.hibernate.query.TypedParameterValue", classLoader)) {
327+
328+
try {
329+
330+
Class<?> typeParameterValue = ClassUtils.forName("org.hibernate.query.TypedParameterValue", classLoader);
331+
332+
if (typeParameterValue.isInstance(value)) {
333+
return "";
334+
}
335+
} catch (ClassNotFoundException | LinkageError o_O) {
336+
return value;
337+
}
338+
}
339+
340+
return value;
341+
}
342+
313343
/**
314344
* Holds the PersistenceProvider specific interface names.
315345
*

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package org.springframework.data.jpa.repository.query;
1717

18+
import jakarta.persistence.criteria.CriteriaBuilder;
19+
import jakarta.persistence.criteria.ParameterExpression;
20+
1821
import java.util.ArrayList;
1922
import java.util.Arrays;
2023
import java.util.Collection;
@@ -24,9 +27,6 @@
2427
import java.util.function.Supplier;
2528
import java.util.stream.Collectors;
2629

27-
import jakarta.persistence.criteria.CriteriaBuilder;
28-
import jakarta.persistence.criteria.ParameterExpression;
29-
3030
import org.springframework.data.jpa.provider.PersistenceProvider;
3131
import org.springframework.data.repository.query.Parameter;
3232
import org.springframework.data.repository.query.Parameters;
@@ -245,14 +245,14 @@ public Object prepare(Object value) {
245245

246246
switch (type) {
247247
case STARTING_WITH:
248-
return String.format("%s%%", escape.escape(value.toString()));
248+
return String.format("%s%%", escape.escape(PersistenceProvider.condense(value).toString()));
249249
case ENDING_WITH:
250-
return String.format("%%%s", escape.escape(value.toString()));
250+
return String.format("%%%s", escape.escape(PersistenceProvider.condense(value).toString()));
251251
case CONTAINING:
252252
case NOT_CONTAINING:
253-
return String.format("%%%s%%", escape.escape(value.toString()));
253+
return String.format("%%%s%%", escape.escape(PersistenceProvider.condense(value).toString()));
254254
default:
255-
return value;
255+
return PersistenceProvider.condense(value);
256256
}
257257
}
258258

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

+7-6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.regex.Matcher;
2828
import java.util.regex.Pattern;
2929

30+
import org.springframework.data.jpa.provider.PersistenceProvider;
3031
import org.springframework.data.repository.query.SpelQueryContext;
3132
import org.springframework.data.repository.query.SpelQueryContext.SpelExtractor;
3233
import org.springframework.data.repository.query.parser.Part.Type;
@@ -637,7 +638,7 @@ static class LikeParameterBinding extends ParameterBinding {
637638

638639
/**
639640
* Creates a new {@link LikeParameterBinding} for the parameter with the given name and {@link Type}.
640-
*
641+
*
641642
* @param name must not be {@literal null} or empty.
642643
* @param type must not be {@literal null}.
643644
*/
@@ -648,7 +649,7 @@ static class LikeParameterBinding extends ParameterBinding {
648649
/**
649650
* Creates a new {@link LikeParameterBinding} for the parameter with the given name and {@link Type} and parameter
650651
* binding input.
651-
*
652+
*
652653
* @param name must not be {@literal null} or empty.
653654
* @param type must not be {@literal null}.
654655
* @param expression may be {@literal null}.
@@ -718,14 +719,14 @@ public Object prepare(@Nullable Object value) {
718719

719720
switch (type) {
720721
case STARTING_WITH:
721-
return String.format("%s%%", value);
722+
return String.format("%s%%", PersistenceProvider.condense(value));
722723
case ENDING_WITH:
723-
return String.format("%%%s", value);
724+
return String.format("%%%s", PersistenceProvider.condense(value));
724725
case CONTAINING:
725-
return String.format("%%%s%%", value);
726+
return String.format("%%%s%%", PersistenceProvider.condense(value));
726727
case LIKE:
727728
default:
728-
return value;
729+
return PersistenceProvider.condense(value);
729730
}
730731
}
731732

spring-data-jpa/src/test/java/org/springframework/data/jpa/domain/sample/Customer.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717

1818
import jakarta.persistence.Entity;
1919
import jakarta.persistence.Id;
20+
import lombok.AccessLevel;
21+
import lombok.AllArgsConstructor;
22+
import lombok.Getter;
23+
import lombok.NoArgsConstructor;
24+
import lombok.Setter;
2025

2126
/**
2227
* @author Oliver Gierke
@@ -25,7 +30,7 @@
2530
@Entity
2631
public class Customer {
2732

28-
@Id Long id;
33+
@Id Long id;
2934

30-
String name;
35+
String name;
3136
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jpa.domain.sample;
17+
18+
import jakarta.persistence.Entity;
19+
import jakarta.persistence.GeneratedValue;
20+
import jakarta.persistence.Id;
21+
import lombok.AccessLevel;
22+
import lombok.Data;
23+
import lombok.NoArgsConstructor;
24+
25+
/**
26+
* @author Greg Turnquist
27+
*/
28+
@Entity
29+
@NoArgsConstructor(access = AccessLevel.PROTECTED)
30+
@Data
31+
public class EmployeeWithName {
32+
33+
@Id
34+
@GeneratedValue private Integer id;
35+
private String name;
36+
37+
public EmployeeWithName(String name) {
38+
39+
this();
40+
this.name = name;
41+
}
42+
}

0 commit comments

Comments
 (0)