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); }
PS:Dao使用@Query注解,也会查询到关联对象。