Skip to content

Commit 79af538

Browse files
committed
DATAJPA-1652: Using || (pipes) along with named parameters in custom queries raises an exception
1 parent e14f5b0 commit 79af538

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public abstract class QueryUtils {
9090
// Cc Control
9191
// Cf Format
9292
// P Punctuation
93-
private static final String IDENTIFIER = "[._[\\P{Z}&&\\P{Cc}&&\\P{Cf}&&\\P{P}]]+";
93+
private static final String IDENTIFIER = "[._[\\P{Z}&&\\P{Cc}&&\\P{Cf}&&[\\P{P}&&[^|]]]]+";
9494
static final String COLON_NO_DOUBLE_COLON = "(?<![:\\\\]):";
9595
static final String IDENTIFIER_GROUP = String.format("(%s)", IDENTIFIER);
9696

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2008-2019 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.repository;
17+
18+
import static org.hamcrest.Matchers.*;
19+
import static org.junit.Assert.*;
20+
21+
import org.junit.After;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.data.domain.Page;
27+
import org.springframework.data.domain.PageRequest;
28+
import org.springframework.data.jpa.domain.sample.User;
29+
import org.springframework.data.jpa.repository.custom.UserCustomExtendedRepository;
30+
import org.springframework.data.repository.query.QueryLookupStrategy;
31+
import org.springframework.test.context.ContextConfiguration;
32+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
33+
import org.springframework.transaction.annotation.Transactional;
34+
35+
/**
36+
* Integration test for executing custom finders, thus testing various query lookup strategies.
37+
*
38+
* @see QueryLookupStrategy
39+
* @author Andriy Redko
40+
*/
41+
@RunWith(SpringJUnit4ClassRunner.class)
42+
@ContextConfiguration(locations = "classpath:config/namespace-customfactory-context.xml")
43+
@Transactional
44+
public class UserCustomRepositoryFinderTests {
45+
46+
@Autowired UserCustomExtendedRepository userRepository;
47+
48+
User dave, carter, oliver;
49+
50+
@Before
51+
public void setUp() {
52+
dave = userRepository.save(new User("Dave", "Matthews", "[email protected]"));
53+
carter = userRepository.save(new User("Carter", "Beauford", "[email protected]"));
54+
oliver = userRepository.save(new User("Oliver August", "Matthews", "[email protected]"));
55+
}
56+
57+
@After
58+
public void clearUp() {
59+
userRepository.deleteAll();
60+
}
61+
62+
@Test
63+
public void executesNamedQueryWithPipes() {
64+
Page<User> page = userRepository.findByNamedQueryWithLike("eau", PageRequest.of(0, 10));
65+
assertThat(page.getNumberOfElements(), is(1));
66+
assertThat(page.getTotalElements(), is(1L));
67+
assertThat(page.getTotalPages(), is(1));
68+
assertThat(page.getContent(), hasItems(carter));
69+
}
70+
71+
@Test
72+
public void executesNamedQueryWithConcat() {
73+
Page<User> page = userRepository.findByNamedQueryWithLikeConcat("eau", PageRequest.of(0, 10));
74+
assertThat(page.getNumberOfElements(), is(1));
75+
assertThat(page.getTotalElements(), is(1L));
76+
assertThat(page.getTotalPages(), is(1));
77+
assertThat(page.getContent(), hasItems(carter));
78+
}
79+
}

src/test/java/org/springframework/data/jpa/repository/custom/UserCustomExtendedRepository.java

+10
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
import java.util.List;
1919
import java.util.Optional;
2020

21+
import org.springframework.data.domain.Page;
22+
import org.springframework.data.domain.Pageable;
2123
import org.springframework.data.jpa.domain.sample.User;
24+
import org.springframework.data.jpa.repository.Query;
25+
import org.springframework.data.repository.query.Param;
2226
import org.springframework.transaction.annotation.Transactional;
2327

2428
/**
@@ -37,4 +41,10 @@ public interface UserCustomExtendedRepository extends CustomGenericRepository<Us
3741

3842
@Transactional(readOnly = false, timeout = 10)
3943
Optional<User> findById(Integer id);
44+
45+
@Query(value = "SELECT u FROM User u WHERE u.lastname LIKE '%'||:name||'%'")
46+
Page<User> findByNamedQueryWithLike(@Param("name") String name, Pageable page);
47+
48+
@Query(value = "SELECT u FROM User u WHERE u.lastname LIKE concat('%', :name, '%')")
49+
Page<User> findByNamedQueryWithLikeConcat(@Param("name") String name, Pageable page);
4050
}

0 commit comments

Comments
 (0)