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

相关推荐
禾小西12 分钟前
Spring AI :Spring AI的介绍
java·人工智能·spring
ybwycx21 分钟前
springboot之集成Elasticsearch
spring boot·后端·elasticsearch
qqty12171 小时前
springboot+mybaties项目中扫描不到@mapper注解的解决方法
java·spring boot·mybatis
程途知微1 小时前
AQS 同步器——Java 并发框架的核心底座全解析
java·后端
星辰_mya1 小时前
InnoDB的“身体结构”:页、Buffer Pool与Redo Log的底层奥秘
数据库·mysql·spring·面试·系统架构
iPadiPhone1 小时前
分布式架构的“润滑剂”:RabbitMQ 核心原理与大厂面试避坑指南
分布式·后端·面试·架构·rabbitmq
武子康2 小时前
大数据-255 离线数仓 - Apache Atlas 数据血缘与元数据管理实战指南
大数据·后端·apache hive
javaTodo2 小时前
IntelliJ IDEA 2026.1 上强度了:Spring 运行时 Debug + AI 全面接入,太香了
后端
晴栀ay2 小时前
Generator + RxJS 重构 LLM 流式输出的“丝滑”架构
javascript·后端·llm
下次一定x2 小时前
深度解析 Kratos 客户端服务发现与负载均衡:从 Dial 入口到 gRPC 全链路落地(下篇)
后端·go