当前位置: 首页 > news >正文

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

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

@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;如果不是,则产生第一个值

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

修改后

@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);

http://www.dtcms.com/a/495272.html

相关文章:

  • 国外网站顶部菜单设计电子商务 网站设计
  • 南城县建设局网站北京vi设计公司哪
  • spark性能优化1:通过依赖关系重组优化Spark性能:宽窄依赖集中处理实践
  • 工程师的烹饪 - 空气炸锅菜谱
  • 如何在腾讯云上建设网站手机必备网站
  • Linux:12.线程同步与互斥
  • 泰安网站建设哪家强wordpress m1
  • el-table中控制单列内容多行超出省略及tooltip
  • 构建企业级跨境电商(Temu)财务数据自动化采集系统
  • B-tree索引像字典查词一样工作?那哪些数据库查询它能加速,哪些不能?
  • C++实现二叉树搜索树
  • 网站开发和美工的区别手机域名注册被骗
  • 做模特的网站python语言编程入门
  • GeeLark 9月功能更新回顾
  • C++---ref-qualifier( / )函数的左右值调用的界定
  • vue3:数组的.includes方法怎么使用
  • 网站建设及网页设计企业宣传片公司
  • 132.MIG IP核中没有512M16的只有512M8的如何解决
  • SwiftUI 布局之美:Padding 让界面呼吸感拉满
  • RHCSA-08文本处理工具
  • JSP XML 数据处理
  • “String到Date转换失败”:深挖@RequestBody的日期坑
  • 分布式事务以及Seata(XA、AT模式)
  • 做网站的 简历标识设计网
  • 平台网站建设意见征求表社区类网站开发
  • 电脑零配件行业MES系统:快速实现全过程信息溯源
  • 基于单片机与上位机的智能宠物喂食管理系统设计
  • 新奇特:黑猫警长的纳米世界,忆阻器与神经网络的智慧
  • 【深度学习新浪潮】LLM 大模型压缩落地实践(2025 版)
  • 神经网络之计算图repeat节点