Skip to content

LikeParameterBinding should unwrap value if necessary #2585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.hibernate.jpa.TypedParameterValue;

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 All @@ -48,6 +50,7 @@
* @author Jens Schauder
* @author Diego Krupitza
* @author Greg Turnquist
* @author Yanming Zhou
*/
class StringQuery implements DeclaredQuery {

Expand Down Expand Up @@ -764,6 +767,11 @@ public Type getType() {
@Override
public Object prepare(@Nullable Object value) {

// unwrap value if necessary, see https://github.com/spring-projects/spring-data-jpa/pull/2461
if (value instanceof TypedParameterValue) {
value = ((TypedParameterValue) value).getValue();
}

if (value == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

import org.assertj.core.api.Assertions;
import org.assertj.core.api.SoftAssertions;
import org.hibernate.jpa.TypedParameterValue;
import org.hibernate.type.StringType;
import org.junit.jupiter.api.Test;
import org.springframework.data.jpa.repository.query.StringQuery.InParameterBinding;
import org.springframework.data.jpa.repository.query.StringQuery.LikeParameterBinding;
Expand All @@ -37,6 +39,7 @@
* @author Nils Borrmann
* @author Andriy Redko
* @author Diego Krupitza
* @author Yanming Zhou
*/
class StringQueryUnitTests {

Expand Down Expand Up @@ -101,6 +104,19 @@ void detectsNamedLikeBindings() {
assertThat(binding.getType()).isEqualTo(Type.ENDING_WITH);
}

@Test // GH-2548
void likeParameterBindingShouldUnwrapValueIfNecessary() {

StringQuery query = new StringQuery("select u from User u where u.firstname like %:firstname%", false);
List<ParameterBinding> bindings = query.getParameterBindings();
LikeParameterBinding binding = (LikeParameterBinding) bindings.get(0);

assertThat(binding.prepare("test")).isEqualTo("%test%");
assertThat(binding.prepare(new TypedParameterValue(StringType.INSTANCE, "test"))).isEqualTo("%test%");
assertThat(binding.prepare(null)).isNull();
assertThat(binding.prepare(new TypedParameterValue(StringType.INSTANCE, null))).isNull();
}

@Test // DATAJPA-461
void detectsNamedInParameterBindings() {

Expand Down