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

相关推荐
想摆烂的不会研究的研究生17 小时前
每日八股——Redis(1)
数据库·经验分享·redis·后端·缓存
毕设源码-郭学长17 小时前
【开题答辩全过程】以 基于SpringBoot技术的美妆销售系统为例,包含答辩的问题和答案
java·spring boot·后端
梨落秋霜17 小时前
Python入门篇【文件处理】
android·java·python
N***H48617 小时前
springcloud springboot nacos版本对应
spring boot·spring·spring cloud
Java 码农18 小时前
RabbitMQ集群部署方案及配置指南03
java·python·rabbitmq
哈库纳玛塔塔18 小时前
放弃 MyBatis,拥抱新一代 Java 数据访问库
java·开发语言·数据库·mybatis·orm·dbvisitor
追逐时光者18 小时前
精选 10 款 .NET 开源免费、功能强大的 Windows 效率软件
后端·.net
追逐时光者18 小时前
一款开源、免费的 WPF 自定义控件集
后端·.net
S***q37718 小时前
Spring Boot管理用户数据
java·spring boot·后端
BD_Marathon18 小时前
SpringBoot——辅助功能之切换web服务器
服务器·前端·spring boot