본문 바로가기
Application/Spring

[JPA] Spring Data JPA containing 관련 에러

by 윤루트 2022. 5. 5.

프로젝트 진행 중 검색 기능을 구현하는 과정에 발생한 일이다.

 

/api/search?keyword="검색어"

RequestParam으로 넘어온 검색어를 포함한 데이터를 찾기 위해 Spring Data JPA를 사용했고 like 쿼리문을 사용하기 위해 다음과 같이 containing을 사용하여 작성했다.

public interface SearchRepository extends JpaRepository<Post, Long> {

	List<Post> findByTitleContaining(String keyword);
}

다음과 같이 임의로 세 개의 post를 저장하고

private void savePost() {
	Post post = new Post(1L, "유교수님 운영체제 전공책 팔아요~");
	Post post2 = new Post(2L, "유교수님 운영체제책 팔아요~");
	Post post3 = new Post(3L, "운영체제 책 팔아요~");
	searchRepository.save(post);
	searchRepository.save(post2);
	searchRepository.save(post3);
}

 

'운영체제'를 검색하면 해당 단어가 포함된 결과가 잘 나오는 것을 알 수 있다.

swagger-ui

이제 띄어쓰기를 포함한 검색을 해보자

 

예를들어 '운영체제 전공책'을 검색한다고 하자.

생각나는 로직은 다음과 같다.

1. RequestParam으로 검색어를 받는다.

2. 공백을 기준으로 split한다.

3. split한 문자열 배열을 돌면서 각 단어별로 searchRepository.findByTitleContaining(word); 로 Posts를 찾아 저장하여 응답한다.

 

List<String> keywords = parseKeyword(keyword);
keywords.forEach(word -> {
	List<Post> searchedPosts = searchRepository.findByTitleContaining(word);
	posts.addAll(searchedPosts);
});

이때 3번에서 문제가 생긴다.

'운영체제'에 대해서는 잘 찾아지지만, 다음 루프에서 '전공책' 검색 시 다음과 같은 에러가 발생한다.

 

2022-05-05 01:04:10.976 ERROR 1567 --- [nio-1234-exec-9] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [\] did not match expected type [java.lang.String (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [\] did not match expected type [java.lang.String (n/a)]] with root cause

java.lang.IllegalArgumentException: Parameter value [\] did not match expected type [java.lang.String (n/a)]
at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:54) ~[hibernate-core-5.6.7.Final.jar:5.6.7.Final]
at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:27) ~[hibernate-core-5.6.7.Final.jar:5.6.7.Final]

...

 

일주일 간 삽질의 결론은 다음과 같다.

https://github.com/spring-projects/spring-data-jpa/issues/2472

 

Issue with spring-data "startingWith" and hibernate 5.6.7: Parameter value [\] did not match expected type [java.lang.String (n/

Hello, I am trying to fetch some entity using a “find all by property starting with” query which amount to a CriteriaQuery using javax.persistence.criteria.CriteriaBuilder.like(Expression, String, ...

github.com

spring-data-jpa의 issue

spring-boot 2.6.5 버전에서 최신 hibernate가 사용되는데, 최신 hibernate에서 생긴 버그인 것 같다.

build.gradle을 확인해 보니, 역시나 2.6.5 버전으로 되어있었다.

 

결론은 버전 다운이다.

spring-boot 2.6.4로 변경 후 실행해본 결과 다음과 같이 결과가 잘 나온 것을 알 수 있다.

swagger-ui

 

같이 알아봐주신 코기님께 감사를 표합니다.

https://www.ecsimsw.com/

 

ecsimsw

 

www.ecsimsw.com

 

댓글