我有一个对商品(id,名称)具有oneToMany的订单(id,名称):
如何使用CriteriaBuilder仅提取名称为“香蕉”的订单:
@Override
public Order orderQuery() {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery criteriaQuery = cb.createQuery(Order.class);
Root root = criteriaQuery.from(Order.class);
root.fetch("items", JoinType.INNER);
CriteriaQuery d = criteriaQuery.select(root);
criteriaQuery.where(cb.equal(root.get("item.name"), "banana"));
return (Order)this.entityManager.createQuery(criteriaQuery).getSingleResult());
}
我得到了错误:
Unable to locate Attribute with the the given name [item.name] on
答案 0 :(得分:0)
您需要使用Join。围绕这些内容:
Join< Order ,Item> joinItems = root.join("items");
criteriaQuery.where(cb.equal(joinItems.get("item.name"), "banana"));
或者,您可以执行以下操作:
criteriaQuery.where(cb.equal(root.get("items").get("name"), "banana"));