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

相关推荐
雨中飘荡的记忆37 分钟前
大流量下库存扣减的数据库瓶颈:Redis分片缓存解决方案
java·redis·后端
开心就好20252 小时前
UniApp开发应用多平台上架全流程:H5小程序iOS和Android
后端·ios
悟空码字2 小时前
告别“屎山代码”:AI 代码整洁器让老项目重获新生
后端·aigc·ai编程
小码哥_常2 小时前
大厂不宠@Transactional,背后藏着啥秘密?
后端
奋斗小强2 小时前
内存危机突围战:从原理辨析到线上实战,彻底搞懂 OOM 与内存泄漏
后端
小码哥_常3 小时前
Spring Boot接口防抖秘籍:告别“手抖”,守护数据一致性
后端
心之语歌3 小时前
基于注解+拦截器的API动态路由实现方案
java·后端
None3213 小时前
【NestJs】基于Redlock装饰器分布式锁设计与实现
后端·node.js
初次攀爬者3 小时前
Kafka + KRaft模式架构基础介绍
后端·kafka
洛森唛3 小时前
Elasticsearch DSL 查询语法大全:从入门到精通
后端·elasticsearch