2012-09-18 42 views
2

我似乎無法運行這個簡單的查詢。我有一個ID列表,我想從數據庫中檢索這個ID的所有行。JPA從列表中選擇

List<String> uniqueIds is my list. 

我已經試過String q = "SELECT item FROM Tenant item WHERE item.id IN (:"+uniqueIds+")";

我也曾經嘗試這樣做沒有:和()。他們沒有工作。我究竟做錯了什麼。我也嘗試過(String uniqueId:uniqueIds){+ = uniqueId +「,」;「}; }

並使用「in」以及!

謝謝。

回答

7

你應該將參數綁定到查詢。參數是匿名的(?)或命名(:parameterName):

TypedQuery<Tenant> query = 
    em.createQuery("select item from Tenant item where item.id in :ids", Tenant.class); 
query.setParameter("ids", uniqueIds); 
List<Tenant> result = query.getResultList(); 
+0

讓我試試,明天。我會盡快給您回覆。謝謝。 – Sara

+0

這應該有更多upvotes。請注意,當你想使用LISTS時,你不能使用匿名參數。這已被棄用,並會導致異常。始終使用命名參數。 – Goot