SpringDataJPA笔记(13)-Union查询

这篇博客介绍了在SpringDataJPA中如何进行Union查询,探讨了通过Inheritance注解和子查询两种方法。重点强调了使用Inheritance注解时,由于需要保证ID唯一性,因此不适用于自增ID。同时,文章指出子查询方式在处理没有统一唯一主键的联合查询时更为灵活。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringDataJPA笔记(13)-Union查询

在JPA中,对Union查询可以通过两种方式,一种是通过Inheritance的注解来实现,一种是通过子查询来实现,个人觉得子查询的方式更加灵活一点

来看具体的demo

首先是第一种通过Inheritance的注解

先设置一个基类,包含了要查询出来的属性,这个类并不会生成实际的表

需要注意一点 如果使用这个注解,id不能使用自增长,因为id在多个表中需要保证唯一性
@Data
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class UnionBaseEntity implements Serializable {

    private static final long serialVersionUID = 1846463590189371497L;
    @Id
    private Long id;

    @Column(name = "union_name")
    private String unionName;

    @Column(name = "union_age")
    private Integer unionAge;
}

然后是两张实际要union查询的表

@Data
@Entity
@EqualsAndHashCode(callSuper = true)
@Table(name = "union_one_tb")
public class UnionOneEntity extends UnionBaseEntity {

    @Column(name = "union_one")
    private String unionOne;

}
@Data
@Entity
@EqualsAndHashCode(callSuper = true)
@Table(name = "union_two_tb")
public class UnionTwoEntity extends UnionBaseEntity {

    @Column(name = "union_two")
    private String unionTwo;

}

然后配置相关的repository接口类

public interface UnionBaseRepository extends JpaRepository<UnionBaseEntity, Long>, JpaSpecificationExecutor<UnionBaseEntity>, Serializable {
}
public interface UnionOneRepository extends JpaRepository<UnionOneEntity, Long>, JpaSpecificationExecutor<UnionOneEntity>, Serializable {
}
public interface UnionTwoRepository extends JpaRepository<UnionTwoEntity, Long>, JpaSpecificationExecutor<UnionTwoEntity>, Serializable {
}

先添加几条数据,查看数据库,确实只有两张表

在这里插入图片描述

再查看两张表的数据

在这里插入图片描述
在这里插入图片描述
编写controller,查询数据

    @GetMapping("/list/base")
    public List<UnionBaseEntity> listBase() {
        return unionBaseRepository.findAll();
    }

从swagger页面调用listBase接口

在这里插入图片描述

这里会发现一个问题,id相同的两条记录被覆盖了,所以使用这个注解的时候,需要保证id的唯一性

第二种通过子查询的模式

@Data
@Entity
@Immutable
@Subselect("select concat(\'one_\', a.id) as id, a.union_name as union_name, a.union_age as union_age from union_one_tb a " +
        "union all select concat(\'two_\', b.id) as id, b.union_name as union_name, b.union_age as union_age from union_two_tb b ")
@Synchronize({"union_one_tb", "union_two_tb"})
public class UnionSubEntity implements Serializable {

    private static final long serialVersionUID = -3795682088296075408L;
    @Id
    private String id;

    @Column(name = "union_name")
    private String unionName;

    @Column(name = "union_age")
    private Integer unionAge;
}
public interface UnionSubRepository extends JpaRepository<UnionSubEntity, String>, JpaSpecificationExecutor<UnionSubEntity>, Serializable {
}

编写controller

    @GetMapping("/list/sub")
    public List<UnionSubEntity> listSub(){
        return unionSubRepository.findAll();
    }

从swagger页面调用listSub接口,查看数据

在这里插入图片描述

子查询的方式更为灵活,特别是对于联合查询的数据没有统一的唯一主键时

欢迎关注微信交流
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值