2010-11-14 29 views
0

我遇到了如何在bulkUpdate Hibernate中使用SQL「in」的問題。Hibernate查詢DetachedCriteria VS bulkUpdate SQL使用Collection with with「in」用法問題

例如:

國家是枚舉類型。

使用bulkUpdate問題使用的DetachedCriteria

DetachedCriteria criteria= DetachedCriteria.forClass(Persons.class) 
.add(Property.forName("country").in(countryList)); 
List<Persons> personsList = getHibernateTemplate().findByCriteria(criteria); 

for(Persons persons : personsList){ 
    person.setCountry(Country.China); 
} 

personsDao.updateAll(personsList); 
personsDao.flush(); 

休眠普通的SQL語句

Update Persons SET country = Country.China 
WHERE country IN (Country.HK, Country.Taiwan); 

休眠 - 不工作!

getHibernateTemplate().bulkUpdate(
"select * from Persons where country in (?)", 
new Object[] {countryList}); 

錯誤消息:

[Lcom.model.Persons.Country; cannot be cast to java.lang.Enum] 

但我想這一個。有用。 [不使用對象:我硬編碼]

getHibernateTemplate().bulkUpdate(
"select * from Persons where country in (" + Country.HK + "," + Country.Taiwan +")"); 

這裏的問題是:
1.我們能否 「在」 子句中使用對象[]用?
2.我想知道爲什麼系統知道對象是國家枚舉。

實施例:

getHibernateTemplate().bulkUpdate(
"select * from Persons where country = ?", 
new Object[] {country}); 

==>它的工作和可執行的。

  1. 除了彙集枚舉(上面的例子),列表或字符串[]也不能使用帶有「in」子句的Object。

希望你們能解決我的問題。 謝謝。 -fsloke

回答

0

好吧,我只是有一個簡單的問題。你確定你的countryList是一個國家的名單,而不只是一個縣的對象?如果你在(?)中使用它期望一個集合。