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

相关推荐
萌新小码农‍2 小时前
Spring框架学习day7--SpringWeb学习(概念与搭建配置)
学习·spring·状态模式
Mr Aokey2 小时前
Spring MVC参数绑定终极手册:单&多参/对象/集合/JSON/文件上传精讲
java·后端·spring
14L3 小时前
互联网大厂Java面试:从Spring Cloud到Kafka的技术考察
spring boot·redis·spring cloud·kafka·jwt·oauth2·java面试
地藏Kelvin3 小时前
Spring Ai 从Demo到搭建套壳项目(二)实现deepseek+MCP client让高德生成昆明游玩4天攻略
人工智能·spring boot·后端
一个有女朋友的程序员3 小时前
Spring Boot 缓存注解详解:@Cacheable、@CachePut、@CacheEvict(超详细实战版)
spring boot·redis·缓存
菠萝014 小时前
共识算法Raft系列(1)——什么是Raft?
c++·后端·算法·区块链·共识算法
长勺4 小时前
Spring中@Primary注解的作用与使用
java·后端·spring
wh_xia_jun4 小时前
在 Spring Boot 中使用 JSP
java·前端·spring boot
yuren_xia4 小时前
在Spring Boot中集成Redis进行缓存
spring boot·redis·缓存
小奏技术5 小时前
基于 Spring AI 和 MCP:用自然语言查询 RocketMQ 消息
后端·aigc·mcp