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

相关推荐
侠客行03174 小时前
Mybatis连接池实现及池化模式
java·mybatis·源码阅读
蛇皮划水怪4 小时前
深入浅出LangChain4J
java·langchain·llm
Victor3564 小时前
https://editor.csdn.net/md/?articleId=139321571&spm=1011.2415.3001.9698
后端
Victor3564 小时前
Hibernate(89)如何在压力测试中使用Hibernate?
后端
灰子学技术6 小时前
go response.Body.close()导致连接异常处理
开发语言·后端·golang
老毛肚6 小时前
MyBatis体系结构与工作原理 上篇
java·mybatis
风流倜傥唐伯虎6 小时前
Spring Boot Jar包生产级启停脚本
java·运维·spring boot
Yvonne爱编码6 小时前
JAVA数据结构 DAY6-栈和队列
java·开发语言·数据结构·python
Re.不晚6 小时前
JAVA进阶之路——无奖问答挑战1
java·开发语言
你这个代码我看不懂7 小时前
@ConditionalOnProperty不直接使用松绑定规则
java·开发语言