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);
相关推荐
lojox8 分钟前
C++ 手写 STL list(双向循环链表)原理剖析与实现
c++·链表·list
池央8 分钟前
Windows本地部署Qwen3.8实战:LM Studio、Cherry Studio、远程API与Token鉴权
windows
inrichtt9 小时前
第八课时:SPI总线揭秘:四线全双工通信的奥
c语言·数据结构·stm32·单片机·嵌入式硬件
HPtiger10 小时前
数据结构——选择排序
数据结构·算法·排序算法
丑梨数据11 小时前
信创环境里跑文档工具:格式兼容与本地合规的几个坑
数据结构·人工智能·算法·决策树·排序算法
烂蜻蜓11 小时前
Pandas入门教程(四):Series数据结构——创建、索引与常用方法
数据结构·pandas
泡海椒12 小时前
JQuick-SQL 快速开始:注册内存表并执行查询
数据库·windows·sql
钱栈up15 小时前
耳机音效不对,我顺手查了系统更新,结果挖出个崩溃了整天的服务
windows·shell
xiao77mwq15 小时前
Wireshark 在 Windows 上没法按进程抓包,我补了一条路
windows
Helix25016 小时前
MDT自动化部署实战:构建企业级Windows批量装机与PXE网络引导环境
windows·mdt自动化部署·wds网络启动·pxe引导·windows批量装机·任务序列·无人值守安装