2013-04-18 152 views
3

比方說,我有一個這樣的實體:JPA查詢比較列表

public class Class1 { 

private long id; 
    ... 
private List<OneRandomObject> list; 
} 

是否有可能在JPA與條件查詢或命名查詢通過他們的名單比較實體?

類似命名查詢:使用標準的API

select c from Class1 c where c.list=:list 

查詢將正常工作爲好。

回答

1

據我所知,不可能將整個集合過濾爲屬性。

也許你可以得到所有的Class1與the list你的條件一起,並篩選如下結果:

for(Class1 c1 : class1List) 
{ 
    List<OneRandomObject> list = c1.getList(); 
    // compare the list with you param 
    // remove if not match 
} 
+0

是的,我也這麼想,我不想走遍所有列表的路徑,因爲它可以變得討厭,對我來說,更改實體並將它們緊密連接在一起會更容易這個問題更讓我感興趣,看看我能如何處理jpa中的列表比較,因爲它們是我非常容易理解的東西。我將等待看看是否有人有更好的想法。同時感謝你爲你的答案。 – sokie

+0

這將是我想要的方式。 'CollectionUtils'有助於緩解一些收藏痛苦。例如,我使用'CollectionUtils.subtract(list,otherList).isEmpty()'來比較集合。 – Dormouse

1

我不知道這是否你的情況相符,但不會在做什麼你要?

public List<Class1> matchList(List<OneRandomObject> other) { 
     // ... 
     String jpql = "select c from Class1 c where c.list in (:other)" 
     // ... 
    } 

我明白你現在要問什麼。那麼在Hibernate Criteria API中呢?

Criteria crit = session.createQuery(Class1.class); 

    Conjunction junction = Restrictions.conjunction(); 

    for(OneRandomObject o: matches) { 
     junction.add(Restrictions.propertyEq("list", o); 
    } 

    crit.add(junction); 
+0

1.如果在括號中添加括號:other,則會出現類型不匹配,說明您試圖將ArrayList放入OneRandomObject中。 2.如果沒有括號,它會起作用,但你會得到一個與c.list中任何元素匹配的元素的行。所以如果你想確保列表中的所有元素都匹配,這是不行的。 – Dormouse