Java 如何在列表中使用 HibernateTemplate.find(...)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23804678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:16:31 来源:igfitidea点击:
how to use HibernateTemplate.find(…) with list
提问by Minion
I am new to Hibernate. earlier i am trying using the following query
我是 Hibernate 的新手。早些时候我尝试使用以下查询
this.getHibernateTemplate()
find("select distinct ci.customer " +
"from CustomerInvoice ci " +
"where ci.name = ? and ci.id in ? ",name,ids);
Where ids is the list of id.It is throwing classCastException. Can someone tell me the solution with reason
其中 ids 是 id 的列表。它正在抛出 classCastException。有人可以告诉我有理由的解决方案吗
Exception:
例外:
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.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.doInHibernate(HibernateTemplate.java:921)
at org.springframework.orm.hibernate3.HibernateTemplate.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)
回答by Vlad Mihalcea
Try this:
尝试这个:
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();
}
});
- First of all I think you need to wrap the IN query parameter in (...)
- You can try with the find() method but if that doesn't know how to apply the list parameter type, you can fall-back to setting it explicitly with "setParameterList" like in my example.
- 首先,我认为您需要将 IN 查询参数包装在 (...)
- 您可以尝试使用 find() 方法,但如果它不知道如何应用列表参数类型,您可以回退到使用“setParameterList”显式设置它,就像我的示例一样。
回答by Nirav Dhinoja
Try This your error will be solved:
试试这个你的错误将得到解决:
this.getHibernateTemplate()
find("select distinct ci.customer " +
"from CustomerInvoice ci " +
"where ci.name = ? and ci.id in ?",new Object[]{name,ids});