2010-11-22 7 views
5

que estoy tratando de ejecutar esta consulta: Código:primavera lista de plantillas de hibernación como parámetro

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

con los identificadores como una lista, id es de tipo long

al ejecutar me sale excepción

Código:

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 
+0

espero que '' no es ids' list' –

+0

que es. está escrito en la pregunta. Pero debería funcionar – Bozho

+0

org.life.java ¿por qué lo deseas? es List , puedo usar cualquier otra colección ... – Hurda

Respuesta

7

Además de la respuesta de mR_fr0g, éste también funciona:

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

¡Tres hurras para una sintaxis más corta! No se deje engañar por pensar que necesita elaboradores de cadenas para los predicados IN. –

9

Si desea añadir una lista a una en la cláusula es bes t para usar un parámetro nombrado. Esto se hace así.

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

podría utilizar el criterio API de Hibernate, tiene una llamada "en" Restricción.

Referencia:

BTW. ¡tenga en cuenta los casos en que la colección de ids está vacía! (No sólo si se utiliza la API de criterios)

1

Puede utilizar la lista de parámetros a inlcude en su consulta con 'IN' y '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(); 
0
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(); 
Cuestiones relacionadas