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

相关推荐
一头生产的驴11 分钟前
java整合itext pdf实现自定义PDF文件格式导出
java·spring boot·pdf·itextpdf
zzywxc78721 分钟前
AI 正在深度重构软件开发的底层逻辑和全生命周期,从技术演进、流程重构和未来趋势三个维度进行系统性分析
java·大数据·开发语言·人工智能·spring
程序员张33 小时前
SpringBoot计时一次请求耗时
java·spring boot·后端
llwszx6 小时前
深入理解Java锁原理(一):偏向锁的设计原理与性能优化
java·spring··偏向锁
程序员岳焱9 小时前
Java 与 MySQL 性能优化:Java 实现百万数据分批次插入的最佳实践
后端·mysql·性能优化
麦兜*9 小时前
Spring Boot启动优化7板斧(延迟初始化、组件扫描精准打击、JVM参数调优):砍掉70%启动时间的魔鬼实践
java·jvm·spring boot·后端·spring·spring cloud·系统架构
KK溜了溜了9 小时前
JAVA-springboot 整合Redis
java·spring boot·redis
大只鹅9 小时前
解决 Spring Boot 对 Elasticsearch 字段没有小驼峰映射的问题
spring boot·后端·elasticsearch
ai小鬼头10 小时前
AIStarter如何快速部署Stable Diffusion?**新手也能轻松上手的AI绘图
前端·后端·github
天河归来10 小时前
使用idea创建springboot单体项目
java·spring boot·intellij-idea