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 pagedListForVaild(A entity, Integer currentPage, Integer pageSize,
			Order... orders) throws Exception {
		// 构建查询条件
		Specification sf = new Specification() {
			@Override
			public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
				List list = new ArrayList();
				Join join = root.join("b", JoinType.INNER);
				list.add(cb.equal(join.get("age"), Age));
				return cb.and(list.toArray(p));
			}
		};

		return super.gerPageResult(currentPage, pageSize, sf, orders);

	}

PS:Dao使用@Query注解,也会查询到关联对象。

你可能感兴趣的:(Java,ORM,spring)