JPA 連表查詢
阿新 • • 發佈:2019-01-31
edi for log color false except and urn spec
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 連表查詢