From d64edde007b14dc7acde0d6c73c71c6c4963b511 Mon Sep 17 00:00:00 2001 From: Thomas Darimont Date: Tue, 19 May 2015 12:44:19 +0200 Subject: [PATCH] DATAJPA-712 - Fix binding of SpEL parameters with IN-clause. We now ensure that all the SpEL parameters are correctly substituted when used together with an IN-clause. Previously we incorrectly used the initial query as the result again which then only substituted the very last parameter correctly. Original pull request: #148. --- .../jpa/repository/query/StringQuery.java | 3 +-- .../query/StringQueryUnitTests.java | 26 ++++++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/springframework/data/jpa/repository/query/StringQuery.java b/src/main/java/org/springframework/data/jpa/repository/query/StringQuery.java index 7946257eb..a688afa0e 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/StringQuery.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/StringQuery.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2014 the original author or authors. + * Copyright 2013-2015 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. @@ -255,7 +255,6 @@ class StringQuery { checkAndRegister(new InParameterBinding(parameterName, expression), bindings); } - result = query; break; case AS_IS: // fall-through we don't need a special parameter binding for the given parameter. diff --git a/src/test/java/org/springframework/data/jpa/repository/query/StringQueryUnitTests.java b/src/test/java/org/springframework/data/jpa/repository/query/StringQueryUnitTests.java index 6f1967008..1a0b7530d 100644 --- a/src/test/java/org/springframework/data/jpa/repository/query/StringQueryUnitTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/query/StringQueryUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2014 the original author or authors. + * Copyright 2013-2015 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. @@ -312,7 +312,31 @@ public class StringQueryUnitTests { public void rejectsDifferentBindingsForRepeatedParameter2() { new StringQuery("select u from User u where u.firstname like ?1 and u.lastname like %?1"); } + + /** + * @see @DATAJPA-712 + */ + @Test + public void shouldReplaceAllNamedExpressionParametersWithInClause() { + + StringQuery query = new StringQuery("select a from A a where a.b in :#{#bs} and a.c in :#{#cs}"); + String queryString = query.getQueryString(); + + assertThat(queryString, is("select a from A a where a.b in :__$synthetic$__1 and a.c in :__$synthetic$__2")); + } + /** + * @see @DATAJPA-712 + */ + @Test + public void shouldReplaceAllPositionExpressionParametersWithInClause() { + + StringQuery query = new StringQuery("select a from A a where a.b in ?#{#bs} and a.c in ?#{#cs}"); + String queryString = query.getQueryString(); + + assertThat(queryString, is("select a from A a where a.b in ?1 and a.c in ?2")); + } + private void assertPositionalBinding(Class bindingType, Integer position, ParameterBinding expectedBinding) {