开发者

Hibernate Query DetachedCriteria VS bulkUpdate SQL using Collection with "in" Calause Problem

I facing a problem on how to use SQL "in" in bulkUpdate Hibernate.

Example :

Country is an enum type.

Normal SQL statement

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

Hibernate using 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();

Hibernate using bulkUpdate problem - Does not work!

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

Error message:

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

But I tried this one. It works. [ Not using Object : I hardcode it]

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

Here the Question:

1. Can we use Object [] with "in" Clause?

2. I wonder why the system know the Object is a Country Enum.

Example :

getHibernateTemplate().bulkUpdate(
开发者_Python百科"select * from Persons where country = ?",
 new Object[] {country});

==> It working and executable.

  1. Beside Collection of Enum ( example above ), List or String [] also not working using Object with "in" Clause.

Hope you guys can solve my problem. Thanks. -fsloke


ok, I just have a simple question. Are you sure that your countryList is a List of Country and not just a single County object? If you use in (?) It expects a collection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜