JPA 连表查询
Posted hanjun0612
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JPA 连表查询相关的知识,希望对你有一定的参考价值。
A表和B表
@Entity @Table(name = "A", schema = "kps", catalog = "kps") @DynamicUpdate public class A implements java.io.Serializable { private String aUUID; //关联B private B b; @OneToOne(fetch = FetchType.EAGER, optional = true) @NotFound(action = NotFoundAction.IGNORE) @JoinColumn(name = "aUUID", referencedColumnName = "aUUID", insertable = false, updatable = false) public B getB() { return b; } public void setB(B b) { this.b= b; } } @Entity @Table(name = "B", schema = "kps", catalog = "kps") @DynamicUpdate public class B implements java.io.Serializable { private String bUUID; private Integer age; }
JPA查询时
@Override public PageResult<A> pagedListForVaild(A entity, Integer currentPage, Integer pageSize, Order... orders) throws Exception { // 构建查询条件 Specification<A> sf = new Specification<A>() { @Override public Predicate toPredicate(Root<A> root, CriteriaQuery<?> query, CriteriaBuilder cb) { List<Predicate> list = new ArrayList<Predicate>(); Join<A, B> join = root.join("b", JoinType.INNER); list.add(cb.equal(join.<String>get("age"), Age)); return cb.and(list.toArray(p)); } }; return super.gerPageResult(currentPage, pageSize, sf, orders); }
以上是关于JPA 连表查询的主要内容,如果未能解决你的问题,请参考以下文章