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 {
@C