JPA 用 List 入参在 @Query中报错 unexpected AST node: {vector}

遇到了一个 JPA 查询的问题 ,我原来的JPA语句是这样的

java 复制代码
@Query(value = " select s from SmsCountEntity  s where (?1 is null or s.areaType in (?1)) order by s.dateCreate desc ")
Page<SmsCountDTO> findSmsCountPage( List<Integer> areaList,Pageable pageable);

看了项目其他也是这样的写法,但还是报错了,喵喵喵?不科学啊。

"message": "org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: {vector} select new com.dfl.ycp3.enquiry.dto.SmsCountDTO(s.shopCode,s.shortName,s.areaType) from com.dfl.ycp3.enquiry.entity.SmsCountEntity s where (:x4_0, :x4_1, :x4_2 is null or s.areaType in (:x4_0, :x4_1, :x4_2)) order by s.dateCreate desc , s.id desc;

很明显我大概知道是入参的 List<Integer> areaList 问题,因为 JPA 语句中会判断入参的 List<Integer> areaList 是否为空,就是 ?1 is null ,但是加上去了在测试的时候就报错了。

去网上搜索,发生这种错误原因有这两种:

第一种

当将数组中的数据放在列表中且没有括号时,用于从列表中搜索数据库的查询语法将是错误的。

例如:

把 s.areaType in (?1) 写成 s.areaType in ?1

因为没把参入用括号括起来,会有语法错误。

第二种

用 coalesce 判断 null

例如:where (coalesce(?1, null) is null or s.areaType in (?1))

因为如果向量为空,则可以产生null;如果不是,则产生第一个值

显然我是第二种情况,诶,搞了我一早上,终于解决了!!!

修改后

java 复制代码
@Query(value = " select new com.dfl.ycp3.enquiry.dto.SmsCountDTO(s.areaType) from SmsCountEntity  s where (coalesce(?1, null) is null or s.areaType in (?1)) and (coalesce(?2, null) is null or s.cityCode in (?2)) order by s.dateCreate desc ")
Page<SmsCountDTO> findSmsCountPage( List<Integer> areaList, Pageable pageable);
相关推荐
MC皮蛋侠客11 小时前
Redis 系列(一):全景与最小闭环——从 `SET` 命令到内存数据结构
数据结构·数据库·redis
天天进步201511 小时前
UI-TARS 源码解析 #22:二次开发实战:用 UI-TARS 做一个简单的 Windows 自动操作 Agent
windows·ui
疯狂打码的少年11 小时前
【数据结构】队列:定义、顺序队列与链式队列
数据结构·笔记
啊哈一半醒11 小时前
Windows 虚拟机搭建 CentOS 全过程(2026 最新版 + 踩坑解决)
linux·windows·centos
new_zhou11 小时前
C++ 项目 AI 协作指南(Windows / MSVC 环境)
c++·人工智能·windows
蔬菜_12 小时前
前端转全栈-day5(数组、list、set)
java·前端·数据结构·list
CV-X.WANG13 小时前
从零理解 ORB-SLAM3(二):系统全景、执行上下文与核心数据结构
数据结构
Doraemomo13 小时前
数据结构-二叉树
数据结构
爱跳舞的烤冷面14 小时前
自学嵌入式第20天(数据结构篇——队列)
数据结构
旖旎夜光14 小时前
LeetCode 202:快乐数(双指针问题) —— 题解
数据结构·c++·算法·leetcode·双指针