Spring hibernate template list as a parameter

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

i'm trying to execute this query : Code:

this.getHibernateTemplate()       find("select distinct ci.customer " +              "from CustomerInvoice ci " +               "where ci.id in (?) " , ids); 

with ids as a List, id is of type Long

when executing i get exception

Code:

java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Long  at org.hibernate.type.LongType.set(LongType.java:42)  at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:136)  at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:116)  at org.hibernate.param.PositionalParameterSpecification.bind(PositionalParameterSpecification.java:39)  at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:491)  at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1563)  at org.hibernate.loader.Loader.doQuery(Loader.java:673)  at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)  at org.hibernate.loader.Loader.doList(Loader.java:2220)  at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)  at org.hibernate.loader.Loader.list(Loader.java:2099)  at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)  at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)  at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)  at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)  at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)  at org.springframework.orm.hibernate3.HibernateTemplate$29.doInHibernate(HibernateTemplate.java:849)  at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:372)  at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:840)  at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:836)  at 

回答1:

In addition to mR_fr0g's answer, this one also works:

this.getHibernateTemplate()        findByNamedParam("select distinct ci.customer " +               "from CustomerInvoice ci " +                "where ci.id in (:ids) ", "ids", ids);  


回答2:

If you want to add a list to an in clause it is best to use a named parameter. This is done like so.

Query q = this.getHibernateTemplate().getSession().createQuery("select distinct ci.customer " +              "from CustomerInvoice ci " +               "where ci.id in (:idsParam) "); q.setParameter("idsParam", ids); List<Customer> = q.getResultList(); 


回答3:

You could use the Hibernate Criteria API, it has a so called "in" Restriction.

Reference:

Btw. be aware of cases where the ids collection is empty! (not only if you use the criteria API)



回答4:

You can use parameter list to inlcude in your query with 'IN' and 'setParameterList'

List<Long> ids= new ArrayList<Long>();  Query query = getSession().createQuery("select distinct ci.customer from CustomerInvoice ci where ci.id in (:ids) "); query.setParameterList("ids", ids); query.executeUpdate(); 


回答5:

   ProjectionList projList =    Projections.projectionList().add("customer","customer");      List<Long> ids = ids;    Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession()       .createCriteria(CustomerInvoice.class)       .add(Restrictions.in("id",ids))       .setProjection(projList); List<Long> listOfIds = criteria.list(); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!