一对多查询,以多的一方的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;
}
@Table(name = "apply")
public class Apply extends IdEntity {
private RefundBill refundBill;
@ManyToOne
@JoinColumn(name = "refund_bill_id", updatable = true)
public RefundBill getRefundBill() {
return refundBill;
this.refundBill = 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()]));
}
}
@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;
}
}
@Table(name = "apply")
public class Apply extends IdEntity {
private RefundBill refundBill;
@ManyToOne
@JoinColumn(name = "refund_bill_id", updatable = true)
public RefundBill getRefundBill() {
return refundBill;
}
this.refundBill = 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()]));
}
}