Jpa使用union all

前言

Hql目前不支持union all语法,目前都是自定义SQL语句去做的

union all使用

定义实体类

less 复制代码
@Data
@Entity
@Table(name = "bank_outbound_work_order")
public class BankOutboundWorkOrder  {

    @Id
    private Long id;

   

    private String applicantRemark;


    private String receiveRemark;


    private Long orgId;

    private LocalDateTime createTime;

}

定义实体类

less 复制代码
@Data
@Entity
@FieldNameConstants
@Table(name = "bank_inbound_work_order")
public class BankInboundWorkOrder extends FullAuditedEntity {




    private Long orgId;

    private LocalDateTime appointmentDate;


    private String applicationInformation;

    private String creator;


    @Enumerated(EnumType.STRING)
    private BankInboundWorkOrderStatusEnum workOrderStatus;

    private String remark;
    
      private LocalDateTime createTime;

}

dao层写法

less 复制代码
@Query(value = " select id, orgId as orgId, applicationInformation as applicationInformation, workOrderStatus as workOrderStatus, createTime as createTime from (" +
        "select  inBound.id as id, inBound.org_id as orgId, inBound.application_information as applicationInformation, inBound.work_order_status as workOrderStatus, inBound.create_time as createTime " +
        " from bank_inbound_work_order inBound where inBound.work_order_status not in :workOrderList " +
        " union all select outBound.id as id, outBound.org_id as orgId, outBound.applicant_remark as applicationInformation, outBound.work_order_status as workOrderStatus, outBound.create_time as createTime " +
        " from bank_outbound_work_order outBound where outBound.work_order_status not in :workOrderList" +
        ") t",
        countQuery = "select count(*) from (" +
                "select  inBound.id as id " +
                " from bank_inbound_work_order inBound where inBound.work_order_status not in :workOrderList " +
                " union all select outBound.id as id " +
                " from bank_outbound_work_order outBound where outBound.work_order_status not in :workOrderList  " +
        ") t",
        nativeQuery = true)
Page<BankHomePageWorkOrderDto> getWorkOrder(@Param("workOrderList") List<String> workOrderList, Pageable pageable);

这里要注意一下BankHomePageWorkOrderDto接受类的写法,写成字段接收会报有问题

css 复制代码
No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [org.luban.module.core.dto.saas.bank.BankHomePageWorkOrderDto]

这个时候要写成

csharp 复制代码
public interface BankHomePageWorkOrderDto {


    Long getId();

    Long getOrgId();

    String getApplicationInformation();

    BankWorkOrderStatusEnum getWorkOrderStatus();

    LocalDateTime getCreateTime();
}

才能转换成实体类

总结

Jpa使用hql不能使用union all,目前只有使用自定义sql,本文使用是Springboot2.3.x以及Springboot2.7.x

相关推荐
jinmo_C++3 分钟前
数据结构_深入理解堆(大根堆 小根堆)与优先队列:从理论到手撕实现
java·数据结构·算法
克莱恩~莫雷蒂13 分钟前
Spring Boot 中 controller层注解
java·spring boot·后端
showker17 分钟前
ecstore等产品开启缓存-后台及前台不能登录原因-setcookie+session问题
java·linux·前端
Excuse_lighttime38 分钟前
排序数组(快速排序算法)
java·数据结构·算法·leetcode·eclipse·排序算法
追逐时光者39 分钟前
分享 4 款基于 .NET 开源免费、实用的文件搜索工具,效率提升利器!
后端·.net
程序新视界42 分钟前
什么是OLTP ,MySQL是如何支持OLTP的?
数据库·后端·mysql
songroom1 小时前
dbpystream webapi: 一次clickhouse数据从系统盘迁至数据盘的尝试
后端·clickhouse·阿里云
whycthe1 小时前
c++竞赛常用函数
java·开发语言
Violet_YSWY1 小时前
final是干嘛的
java·开发语言
RickyWasYoung1 小时前
【matlab】字符串数组 转 double
android·java·javascript