Java Spring hibernate 模板列表作为参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4245625/
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
Spring hibernate template list as a parameter
提问by Hurda
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
以 ids 作为列表,id 的类型为 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.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
采纳答案by axtavt
In addition to mR_fr0g's answer, this one also works:
除了 mR_fr0g 的答案,这个也有效:
this.getHibernateTemplate()
findByNamedParam("select distinct ci.customer " +
"from CustomerInvoice ci " +
"where ci.id in (:ids) ", "ids", ids);
回答by mR_fr0g
If you want to add a list to an in clause it is best to use a named parameter. This is done like so.
如果要向 in 子句添加列表,最好使用命名参数。这是这样做的。
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();
回答by Ralph
You could use the Hibernate Criteria API, it has a so called "in" Restriction.
您可以使用 Hibernate Criteria API,它有一个所谓的“in”限制。
Reference:
参考:
Btw. be aware of cases where the ids collection is empty! (not only if you use the criteria API)
顺便提一句。请注意 ids 集合为空的情况!(不仅在您使用标准 API 时)
回答by borchvm
You can use parameter list to inlcude in your query with 'IN' and 'setParameterList'
您可以使用参数列表将“IN”和“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();
回答by Preetham
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();