【发布时间】:2013-02-01 12:38:45
【问题描述】:
我正在使用 JPA 并尝试选择父母 (Category) 的给定 List 的所有子实体 (Product)。关系是Category OneToMany Product。我想将其保留为一个查询,而不是像 product.get("category") == category.get(0) || product.get("category") == category.get(1) || ... 这样创建 Predicate。
我尝试了以下代码,但这似乎不起作用(如底部的堆栈所示)。有没有人对如何实现这一点提出建议?
代码
public List<Product> findProductsBy(List<Category> categories) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Product> query = cb.createQuery(Product.class);
Root product = query.from(Product.class);
Predicate predicateCategory = product.get("category").in(categories);
query.select(product).where(predicateCategory);
return em.createQuery(query).getResultList();
}
堆栈
WARNING: Local Exception Stack:
Exception [EclipseLink-6075] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.QueryException
Exception Description: Object comparisons can only use the equal() or notEqual() operators. Other comparisons must be done through query keys or direct attribute level comparisons.
Expression: [
Relation operator [ IN ]
Query Key category
Base (...).Product
【问题讨论】:
标签: java jpa eclipselink