Skip to content

Commit f0216b0

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 cd27b57 commit f0216b0

File tree

7 files changed

+367
-17
lines changed

7 files changed

+367
-17
lines changed

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

+30
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.springframework.lang.Nullable;
4141
import org.springframework.transaction.support.TransactionSynchronizationManager;
4242
import org.springframework.util.Assert;
43+
import org.springframework.util.ClassUtils;
4344
import org.springframework.util.ConcurrentReferenceHashMap;
4445

4546
/**
@@ -329,6 +330,35 @@ public boolean canExtractQuery() {
329330
}
330331
}
331332

333+
/**
334+
* Because Hibernate's {@literal TypedParameterValue} is only used to wrap a {@literal null}, swap it out with an
335+
* empty string for query creation.
336+
*
337+
* @param value
338+
* @return the original value or an empty string.
339+
* @since 3.0
340+
*/
341+
public static Object condense(Object value) {
342+
343+
ClassLoader classLoader = PersistenceProvider.class.getClassLoader();
344+
345+
if (ClassUtils.isPresent("org.hibernate.jpa.TypedParameterValue", classLoader)) {
346+
347+
try {
348+
349+
Class<?> typeParameterValue = ClassUtils.forName("org.hibernate.jpa.TypedParameterValue", classLoader);
350+
351+
if (typeParameterValue.isInstance(value)) {
352+
return "";
353+
}
354+
} catch (ClassNotFoundException | LinkageError o_O) {
355+
return value;
356+
}
357+
}
358+
359+
return value;
360+
}
361+
332362
/**
333363
* Holds the PersistenceProvider specific interface names.
334364
*

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

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

18-
import java.util.ArrayList;
19-
import java.util.Arrays;
20-
import java.util.Collection;
21-
import java.util.Collections;
22-
import java.util.Iterator;
23-
import java.util.List;
18+
import java.util.*;
2419
import java.util.function.Supplier;
2520
import java.util.stream.Collectors;
2621

@@ -241,14 +236,14 @@ public Object prepare(Object value) {
241236

242237
switch (type) {
243238
case STARTING_WITH:
244-
return String.format("%s%%", escape.escape(value.toString()));
239+
return String.format("%s%%", escape.escape(PersistenceProvider.condense(value).toString()));
245240
case ENDING_WITH:
246-
return String.format("%%%s", escape.escape(value.toString()));
241+
return String.format("%%%s", escape.escape(PersistenceProvider.condense(value).toString()));
247242
case CONTAINING:
248243
case NOT_CONTAINING:
249-
return String.format("%%%s%%", escape.escape(value.toString()));
244+
return String.format("%%%s%%", escape.escape(PersistenceProvider.condense(value).toString()));
250245
default:
251-
return value;
246+
return PersistenceProvider.condense(value);
252247
}
253248
}
254249

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;
@@ -689,7 +690,7 @@ static class LikeParameterBinding extends ParameterBinding {
689690

690691
/**
691692
* Creates a new {@link LikeParameterBinding} for the parameter with the given name and {@link Type}.
692-
*
693+
*
693694
* @param name must not be {@literal null} or empty.
694695
* @param type must not be {@literal null}.
695696
*/
@@ -700,7 +701,7 @@ static class LikeParameterBinding extends ParameterBinding {
700701
/**
701702
* Creates a new {@link LikeParameterBinding} for the parameter with the given name and {@link Type} and parameter
702703
* binding input.
703-
*
704+
*
704705
* @param name must not be {@literal null} or empty.
705706
* @param type must not be {@literal null}.
706707
* @param expression may be {@literal null}.
@@ -770,14 +771,14 @@ public Object prepare(@Nullable Object value) {
770771

771772
switch (type) {
772773
case STARTING_WITH:
773-
return String.format("%s%%", value);
774+
return String.format("%s%%", PersistenceProvider.condense(value));
774775
case ENDING_WITH:
775-
return String.format("%%%s", value);
776+
return String.format("%%%s", PersistenceProvider.condense(value));
776777
case CONTAINING:
777-
return String.format("%%%s%%", value);
778+
return String.format("%%%s%%", PersistenceProvider.condense(value));
778779
case LIKE:
779780
default:
780-
return value;
781+
return PersistenceProvider.condense(value);
781782
}
782783
}
783784

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 lombok.AccessLevel;
19+
import lombok.Data;
20+
import lombok.NoArgsConstructor;
21+
22+
import javax.persistence.Entity;
23+
import javax.persistence.GeneratedValue;
24+
import javax.persistence.Id;
25+
26+
/**
27+
* @author Greg Turnquist
28+
*/
29+
@Entity
30+
@NoArgsConstructor(access = AccessLevel.PROTECTED)
31+
@Data
32+
public class EmployeeWithName {
33+
34+
@Id
35+
@GeneratedValue private Integer id;
36+
private String name;
37+
38+
public EmployeeWithName(String name) {
39+
40+
this();
41+
this.name = name;
42+
}
43+
}

0 commit comments

Comments
 (0)