Skip to content

Commit

Permalink
Properly handle null values inside queries using LIKE or CONTAINS.
Browse files Browse the repository at this point in the history
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#
  • Loading branch information
gregturn committed Sep 23, 2022
1 parent cd27b57 commit f0216b0
Show file tree
Hide file tree
Showing 7 changed files with 367 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.springframework.lang.Nullable;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ConcurrentReferenceHashMap;

/**
Expand Down Expand Up @@ -329,6 +330,35 @@ public boolean canExtractQuery() {
}
}

/**
* Because Hibernate's {@literal TypedParameterValue} is only used to wrap a {@literal null}, swap it out with an
* empty string for query creation.
*
* @param value
* @return the original value or an empty string.
* @since 3.0
*/
public static Object condense(Object value) {

ClassLoader classLoader = PersistenceProvider.class.getClassLoader();

if (ClassUtils.isPresent("org.hibernate.jpa.TypedParameterValue", classLoader)) {

try {

Class<?> typeParameterValue = ClassUtils.forName("org.hibernate.jpa.TypedParameterValue", classLoader);

if (typeParameterValue.isInstance(value)) {
return "";
}
} catch (ClassNotFoundException | LinkageError o_O) {
return value;
}
}

return value;
}

/**
* Holds the PersistenceProvider specific interface names.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@
*/
package org.springframework.data.jpa.repository.query;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -241,14 +236,14 @@ public Object prepare(Object value) {

switch (type) {
case STARTING_WITH:
return String.format("%s%%", escape.escape(value.toString()));
return String.format("%s%%", escape.escape(PersistenceProvider.condense(value).toString()));
case ENDING_WITH:
return String.format("%%%s", escape.escape(value.toString()));
return String.format("%%%s", escape.escape(PersistenceProvider.condense(value).toString()));
case CONTAINING:
case NOT_CONTAINING:
return String.format("%%%s%%", escape.escape(value.toString()));
return String.format("%%%s%%", escape.escape(PersistenceProvider.condense(value).toString()));
default:
return value;
return PersistenceProvider.condense(value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.data.jpa.provider.PersistenceProvider;
import org.springframework.data.repository.query.SpelQueryContext;
import org.springframework.data.repository.query.SpelQueryContext.SpelExtractor;
import org.springframework.data.repository.query.parser.Part.Type;
Expand Down Expand Up @@ -689,7 +690,7 @@ static class LikeParameterBinding extends ParameterBinding {

/**
* Creates a new {@link LikeParameterBinding} for the parameter with the given name and {@link Type}.
*
*
* @param name must not be {@literal null} or empty.
* @param type must not be {@literal null}.
*/
Expand All @@ -700,7 +701,7 @@ static class LikeParameterBinding extends ParameterBinding {
/**
* Creates a new {@link LikeParameterBinding} for the parameter with the given name and {@link Type} and parameter
* binding input.
*
*
* @param name must not be {@literal null} or empty.
* @param type must not be {@literal null}.
* @param expression may be {@literal null}.
Expand Down Expand Up @@ -770,14 +771,14 @@ public Object prepare(@Nullable Object value) {

switch (type) {
case STARTING_WITH:
return String.format("%s%%", value);
return String.format("%s%%", PersistenceProvider.condense(value));
case ENDING_WITH:
return String.format("%%%s", value);
return String.format("%%%s", PersistenceProvider.condense(value));
case CONTAINING:
return String.format("%%%s%%", value);
return String.format("%%%s%%", PersistenceProvider.condense(value));
case LIKE:
default:
return value;
return PersistenceProvider.condense(value);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.domain.sample;

import lombok.AccessLevel;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

/**
* @author Greg Turnquist
*/
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Data
public class EmployeeWithName {

@Id
@GeneratedValue private Integer id;
private String name;

public EmployeeWithName(String name) {

this();
this.name = name;
}
}
Loading

0 comments on commit f0216b0

Please sign in to comment.