Using a second query while scrolling causes the ScrollableResults to be closed. Minimal example:
public static void main(String[] args)
{
EntityManager em = getEntityManager();
Query qExtra = em.createNativeQuery("SELECT 1");
Query q = em.createQuery("SELECT row FROM User row");
q.setMaxResults(10);
@SuppressWarnings("rawtypes")
org.hibernate.query.Query hibernateQuery = q.unwrap(org.hibernate.query.Query.class);
ScrollableResults sr = hibernateQuery.scroll();
while(sr.next())
{
qExtra.getSingleResult();
}
em.close();
}
Perhaps I am not supposed to unwrap the JPA2 query like that? WIth Hibernate 5.1.0 and the following code to obtain a ScrollableResults, the example code works as expected:
org.hibernate.Query query = ((org.hibernate.jpa.internal.QueryImpl)q).getHibernateQuery();
ScrollableResults sr = query.scroll(ScrollMode.FORWARD_ONLY);
However, running 5.4.18, it does not appear to matter what query I issue inside the loop, it will close the result set. The resulting exception from 5.4.18:
Thanks for any help! |