ESCAPE 在 Spring JPA 中未被识别

问题描述 投票:0回答:1

我在尝试启动 Spring Boot 应用程序时遇到异常:

“antlr.NoViableAltException:意外令牌:ESCAPE”

代码:

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;


public interface PostRepository extends CrudRepository<Post, Long> {

   @Query("SELECT e FROM Post e WHERE UPPER(e.title) LIKE UPPER(:filterValue ESCAPE '%') OR UPPER(e.content) LIKE UPPER(:filterValue ESCAPE '%') ORDER BY e.title ASC")
   Page<Post> findAllByTitleContainingIgnoreCaseOrContentContainingIgnoreCaseOrderByTitleAsc(@Param("filterValue") String filterValue, Pageable pageable);

}

我正在尝试以能够包含来自服务层的通配符的方式实现查询。这时候我把所有的*换成%传给数据访问层

在我更改为上面之前,sql 输出显示 % 正在被转义,因此不匹配任何记录。

上面是为了防止%字符在查询中被转义的努力,但是我得到了ESCAPE is not recognized的错误。

sql spring jpa spring-data-jpa jpql
1个回答
0
投票

您可以尝试使用

ESCAPE
UPPER
并使用
nativeQuery = true
启用本机查询:

@Query(value = "SELECT e FROM Post e WHERE UPPER(e.title) " +
       "LIKE (UPPER(:filterValue) ESCAPE '%') OR UPPER(e.content) " +
       "LIKE (UPPER(:filterValue) ESCAPE '%') ORDER BY e.title ASC", nativeQuery = true)
Page<Post> findAllByTitleContainingIgnoreCaseOrContentContainingIgnoreCaseOrderByTitleAsc(
    @Param("filterValue") String filterValue, Pageable pageable);

https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods

© www.soinside.com 2019 - 2024. All rights reserved.