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版本

相关推荐
学长毕设5 小时前
基于springboot的食品仓库管理系统
java·spring boot·后端
滴滴答答哒6 小时前
ASP.NET Core + FaceRecognitionDotNet 人脸识别实战:DI 单例、特征向量持久化与 1:N 比对
后端·asp.net
步行cgn6 小时前
FormHttpMessageConverter 详解:Spring MVC 中表单数据的读写转换器
java·spring·mvc
霸道流氓气质8 小时前
智能客服组件:分层意图识别、RAG Advisor 与人工转接状态机的 Vue3 + Spring AI 生产级实战
java·人工智能·spring
captain3768 小时前
MyBatis操作数据库(入门2)
java·数据库·spring boot·后端·spring·mybatis
步行cgn9 小时前
Spring MVC + Thymeleaf 模板解析失败:从 500 错误到页面正常显示
java·spring·mvc
IT_陈寒10 小时前
Java序列化踩坑记:对象循环引用让我加班两小时
前端·人工智能·后端
泡海椒10 小时前
05-雷达图多维画像-信用评级多维画像
后端
王中阳Go10 小时前
5.27 分钟干完后端和全部测试:一场三小时笔试的五个失分点,四个不在代码上
大数据·数据库·人工智能·后端·大语言模型
大哥430910 小时前
Modbus 的"字序":一个配错就变成天文数字,或者干脆变成 0
后端