2013-02-09 39 views
0

選擇我覺得自己像一個小白試圖找出這一個 -春天的JpaTemplate語法從列表

我想知道下面的JPA查詢從號碼列表中選擇正確的語法。 ?

List<Long> personIds = new ArrayList<Long>(); 
personIds.add(1L); 
personIds.add(2L); 
jpaTemplate.find("select count(*) from Person p where p.id in (?) ",personIds); 

當我嘗試,我得到

"java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Long" 
+0

嘗試長賦值給一個列表,在投它'(對象)personIds'。 – Smit 2013-02-09 00:56:33

+0

供參考:'JpaTemplate'已被棄用。看到這裏---> [org.springframework.orm.jpa JpaTemplate類](http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/orm/jpa/JpaTemplate .html) – Smit 2013-02-09 01:05:11

+0

鑄造到對象沒有工作。我只需要運行這一個查詢,這是現有代碼的一部分,所以我不想更改爲更新的實現,因爲我知道我缺少一些非常微不足道的東西。 – user1959268 2013-02-09 01:12:36

回答

0

我固定它通過使用EntityManager的,像這樣 -

Query q = jpaTemplate.getEntityManager().find("select count(*) from Person p where p.id in :personIds") ; 
q.setParameter("personIds",personIds); 
q.getResultSet(); 
0

嘗試設置你的參數JPQL沒有括號。像這樣:

List<Long> personIds = new ArrayList<Long>(); 
personIds.add(1L); 
personIds.add(2L); 
jpaTemplate.find("select count(*) from Person p where p.id in ?",personIds); 

我認爲這應該可以解決您的問題。

+0

它沒有工作。然而,我解決它使用EntityManager像這樣'查詢q = jpaTemplate.getEntityManager()。查找(「選擇計數(*)從個人p其中p.id在:personIds」); q.setParameter( 「personIds」,personIds); q.getResultSet();' – user1959268 2013-02-12 21:55:29

0

您的查詢返回一個長,因爲count,並要