简体   繁体   中英

JPA/Hibernate 4 - getting ScrollableResults

I am using Hibernate 4.2 as my JPA provider. I want to use a ScrollableResults object to process my result set.

My code looks like this, essentially:

javax.persistence.Query q = em.createQuery(...);
org.hibernate.Query query = ((org.hibernate.ejb.QueryImpl)q).getHibernateQuery();
query.setParameter(blah blah);
ScrollableResults sr = query.scroll(ScrollMode.FORWARD_ONLY);

This always throws an exception:

Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Integer
    at org.hibernate.type.descriptor.java.IntegerTypeDescriptor.unwrap(IntegerTypeDescriptor.java:36)
    at org.hibernate.type.descriptor.sql.IntegerTypeDescriptor$1.doBind(IntegerTypeDescriptor.java:57)
    at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:92)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:280)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:275)
    at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:66)
    at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:608)
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1870)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1831)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1811)
    at org.hibernate.loader.Loader.doQuery(Loader.java:899)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
    at org.hibernate.loader.Loader.doList(Loader.java:2516)
    at org.hibernate.loader.Loader.doList(Loader.java:2502)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2332)
    at org.hibernate.loader.Loader.list(Loader.java:2327)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:490)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1268)
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
    at com.example.server.ChartGenerator.getChart()

As an experiment, replacing query.scroll() with query.list() also produces the same exception.

The QueryImpl type is a raw type, but I can't figure out what to put in there, or if that's even what the problem is.

My query returns a date followed by several ints; it does not map directly to a class. I just want an array of objects - I have loads of data to process, and mapping to objects will just slow me down!

Thanks to a good tip from fmodos, the exception was arising because I was calling:

org.hibernate.Query.setParameter("groupids", list);

The problem is resolved by calling one of the following:

org.hibernate.Query.setParameterList("groupids", list);
javax.persistence.Query.setParameter("groupids", list);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM