File tree 2 files changed +24
-0
lines changed
main/java/org/springframework/data/jpa/repository/query
test/java/org/springframework/data/jpa/repository/query
2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change 27
27
import java .util .regex .Matcher ;
28
28
import java .util .regex .Pattern ;
29
29
30
+ import org .hibernate .jpa .TypedParameterValue ;
31
+
30
32
import org .springframework .data .repository .query .SpelQueryContext ;
31
33
import org .springframework .data .repository .query .SpelQueryContext .SpelExtractor ;
32
34
import org .springframework .data .repository .query .parser .Part .Type ;
48
50
* @author Jens Schauder
49
51
* @author Diego Krupitza
50
52
* @author Greg Turnquist
53
+ * @author Yanming Zhou
51
54
*/
52
55
class StringQuery implements DeclaredQuery {
53
56
@@ -764,6 +767,11 @@ public Type getType() {
764
767
@ Override
765
768
public Object prepare (@ Nullable Object value ) {
766
769
770
+ // unwrap value if necessary, see https://github.com/spring-projects/spring-data-jpa/pull/2461
771
+ if (value instanceof TypedParameterValue ) {
772
+ value = ((TypedParameterValue ) value ).getValue ();
773
+ }
774
+
767
775
if (value == null ) {
768
776
return null ;
769
777
}
Original file line number Diff line number Diff line change 22
22
23
23
import org .assertj .core .api .Assertions ;
24
24
import org .assertj .core .api .SoftAssertions ;
25
+ import org .hibernate .jpa .TypedParameterValue ;
26
+ import org .hibernate .type .StringType ;
25
27
import org .junit .jupiter .api .Test ;
26
28
import org .springframework .data .jpa .repository .query .StringQuery .InParameterBinding ;
27
29
import org .springframework .data .jpa .repository .query .StringQuery .LikeParameterBinding ;
37
39
* @author Nils Borrmann
38
40
* @author Andriy Redko
39
41
* @author Diego Krupitza
42
+ * @author Yanming Zhou
40
43
*/
41
44
class StringQueryUnitTests {
42
45
@@ -101,6 +104,19 @@ void detectsNamedLikeBindings() {
101
104
assertThat (binding .getType ()).isEqualTo (Type .ENDING_WITH );
102
105
}
103
106
107
+ @ Test // GH-2548
108
+ void namedLikeBindingsShouldUnwrapValueIfNecessary () {
109
+
110
+ StringQuery query = new StringQuery ("select u from User u where u.firstname like %:firstname%" , false );
111
+ List <ParameterBinding > bindings = query .getParameterBindings ();
112
+ LikeParameterBinding binding = (LikeParameterBinding ) bindings .get (0 );
113
+
114
+ assertThat (binding .prepare ("test" )).isEqualTo ("%test%" );
115
+ assertThat (binding .prepare (new TypedParameterValue (StringType .INSTANCE , "test" ))).isEqualTo ("%test%" );
116
+ assertThat (binding .prepare (null )).isNull ();
117
+ assertThat (binding .prepare (new TypedParameterValue (StringType .INSTANCE , null ))).isNull ();
118
+ }
119
+
104
120
@ Test // DATAJPA-461
105
121
void detectsNamedInParameterBindings () {
106
122
You can’t perform that action at this time.
0 commit comments