0
我正在使用JPA並試圖選擇給定List
父母(Category
)的所有childentities(Product
)。關係是Category OneToMany Product
。我想將它保留在一個查詢中,而不是像product.get("category") == category.get(0) || product.get("category") == category.get(1) || ...
那樣創建Predicate
。Java JPA:Select'ATTRIBUTE is in List <>'
我已經嘗試了下面的代碼,但這似乎不起作用(如在底部的堆棧中所見)。有沒有人有如何完成這一建議?
代碼
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
我確實是想通過ID此刻comparise,將盡快我可以證實這是工作給予的更新。 – Aquillo
你是對的,這個作品完美無缺。 – Aquillo
你也可以試試2.4,我覺得現在支持。 – James