![](/img/trans.png)
[英]Hibernate build query from List<Criterion> with or operators and dynamic Restrictions
[英]match all the contents of a list in restrictions in hibernate query
我想達到上述要求。
我有一個像
public class Test{private String id;private List<Items> items;}
物品類
public class Item{private String name; private String state;}
我想查詢特定限制組中所有項目的狀態。
比方說
像{完成,需要確認,拒絕}之類的狀態
我想查詢{完成,需要確認}組中所有項目的狀態。
如果有一個項目的狀態不在上述狀態中,則不應給出查詢結果。
我檢查這種事情。我想知道我是在正確執行此操作還是方法。
criteria.add(Restrictions.in("item.state", states));
使用您嘗試過的條件:
criteria.add(Restrictions.in("item.state", states));
會給你以下錯誤
org.hibernate.QueryException: could not resolve property: item of: Test
這是因為在Test類中沒有“ item”屬性。
為了使它起作用,您需要為您的商品創建一個別名,並在您的限制中使用它:
Criteria crit = hibSession.createCriteria(Test.class);
crit.createAlias("items", "item_");
crit.add(Restrictions.in("item_.state", states));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.