JPA自定义sql参数为空和postgresql遇到问题

问题描述

今天使用jpa自定义sql时,发现判空处理有问题

less 复制代码
 @Query(value = "select d.user_id from device_grant_auth_item_info d inner join device_grant_auth " +
            " g on d.device_grant_auth_id = g.id inner join sys_device device on d.device_id = device.id" +
            " left join sys_user u on d.user_id = u.id where ((g.grant_auth_type = 'PERPETUAL') or (g.grant_auth_type = 'NORMAL' " +
            " and d.start_time >= CAST(:time AS timestamp) and d.end_time <= CAST(:time AS timestamp)) or (g.grant_auth_type = 'TEMPORALITY' and d.start_time >= CAST(:time AS timestamp) and d.end_time <= CAST(:time AS timestamp) " +
            " and (d.use_count = 0 or d.device_lock_count < d.use_count ) ))" +
            " and  u.user_type in (:userTypeList))  and  d.user_id = :userId) " +
            "and (d.create_id = :createId) group by d.user_id order by d.user_id asc ", nativeQuery = true)
    Page<Long> page(@Param("time") LocalDateTime localDateTime, @Param("userTypeList") List<UserType> userTypeList,
                    @Param("userId") Long userId,
                    @Param("createId") Long createId,
                    Pageable pageable);
}

发现userId等传入null会报错,抛出的异常如下

sql 复制代码
ERROR: COALESCE types bytea and character varying in PostgreSQL

后面使用hql语句解决

less 复制代码
  @Query(value = "select d.userId from DeviceGrantAuthItemInfo d inner join DeviceGrantAuth " +
            " g on d.deviceGrantAuthId = g.id inner join SaasDevice device on d.deviceId = device.id" +
            " left join User u on d.userId = u.id where (g.grantAuthType = 'PERPETUAL' " +
            " or (g.grantAuthType = 'NORMAL' and d.startTime <= :time and d.endTime >= :time) " +
            " or (g.grantAuthType = 'TEMPORALITY' and d.startTime <= :time and d.endTime >= :time and (d.useCount = 0 or d.deviceLockCount < d.useCount ) ))" +
            " and (:userTypeList IS NULL OR u.userType in (:userTypeList))  and (:userId IS NULL or d.userId = :userId) " +
            "and (:createId IS NULL or d.createId = :createId) group by d.userId order by d.userId asc ")
    Page<Long> page(@Param("time") LocalDateTime localDateTime, @Param("userTypeList") List<UserType> userTypeList,
                    @Param("userId") Long userId,
                    @Param("createId") Long createId,
                    Pageable pageable);

本文使用的是JDK14以及Springboot2.3.8版本

相关推荐
uzong5 小时前
技术故障复盘模版
后端
GetcharZp5 小时前
基于 Dify + 通义千问的多模态大模型 搭建发票识别 Agent
后端·llm·agent
桦说编程6 小时前
Java 中如何创建不可变类型
java·后端·函数式编程
IT毕设实战小研6 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
wyiyiyi6 小时前
【Web后端】Django、flask及其场景——以构建系统原型为例
前端·数据库·后端·python·django·flask
一只爱撸猫的程序猿7 小时前
使用Spring AI配合MCP(Model Context Protocol)构建一个"智能代码审查助手"
spring boot·aigc·ai编程
甄超锋7 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
阿华的代码王国7 小时前
【Android】RecyclerView复用CheckBox的异常状态
android·xml·java·前端·后端
Jimmy7 小时前
AI 代理是什么,其有助于我们实现更智能编程
前端·后端·ai编程
AntBlack8 小时前
不当韭菜V1.1 :增强能力 ,辅助构建自己的交易规则
后端·python·pyqt