spring data jpa Specification 多对一检索

来源:互联网 发布:mac上用什么输入法 编辑:程序博客网 时间:2024/05/18 12:29
一对多查询,以多的一方的ID查询一
@Entity
@Table(name = "refund_bill")
public class RefundBill extends IdEntity {
  private List<Apply> applyList = Lists.newArrayList();
@OneToMany(mappedBy = "refundBill")
public List<Apply> getApplyList() {
return applyList;
}
public void setApplyList(List<Apply> applyList) {
this.applyList = applyList;
}

}


@Entity
@Table(name = "apply")
public class Apply extends IdEntity {
   private RefundBill refundBill;
@ManyToOne
@JoinColumn(name = "refund_bill_id", updatable = true)
public RefundBill getRefundBill() {
return refundBill;

}


public void setRefundBill(RefundBill refundBill) {
this.refundBill = refundBill;
}

}


Specification<RefundBill> spec = new Specification<RefundBill>() {
@Override
public Predicate toPredicate(Root<RefundBill> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Join<RefundBill, Apply> joins = root.join("applyList");
List<Predicate> predicates = Lists.newArrayList();
predicates.add(cb.equal(joins.<Long>get("id"), applyId));

return cb.and(predicates.toArray(new Predicate[predicates.size()]));
}
}





0 0
原创粉丝点击