如何使用HibernateTemplate.find(...)与列表

时间:2014-05-22 10:47:57

标签: java hibernate hql

我是Hibernate的新手。我之前尝试使用以下查询

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

其中id是id列表。它正在抛出classCastException。有人可以告诉我有理由的解决方案

例外:

java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String
at org.hibernate.type.descriptor.java.StringTypeDescriptor.unwrap(StringTypeDescriptor.java:40)
at org.hibernate.type.descriptor.sql.VarcharTypeDescriptor$1.doBind(VarcharTypeDescriptor.java:52)
at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:91)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:283)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:278)
at org.hibernate.param.PositionalParameterSpecification.bind(PositionalParameterSpecification.java:68)
at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:578)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1716)
at org.hibernate.loader.Loader.doQuery(Loader.java:801)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
at org.hibernate.loader.Loader.doList(Loader.java:2542)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
at org.hibernate.loader.Loader.list(Loader.java:2271)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:459)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:365)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1268)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:921)
at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:1)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:912) 

2 个答案:

答案 0 :(得分:1)

试试这个:

List customers = getHibernateTemplate().executeFind(new HibernateCallback<List>() {
        @Override
        public List doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createQuery(
                    "select distinct ci.customer " +
                            "from CustomerInvoice ci " +
                            "where ci.name = :name and ci.id in (:ids) "
            );
            query.setParameter("name", name);
            query.setParameterList("ids", ids);
            return query.list();
        }
});
  1. 首先,我认为你需要在(...)
  2. 中包装IN查询参数
  3. 您可以尝试使用find()方法,但如果不知道如何应用列表参数类型,则可以使用&#34; setParameterList&#34;显式设置它。就像我的例子一样。

答案 1 :(得分:0)

试试这个你的错误将会解决:

this.getHibernateTemplate()
      find("select distinct ci.customer " +
         "from CustomerInvoice ci " +
          "where ci.name = ? and ci.id in ?",new Object[]{name,ids});