after we upgrade our project from hibernate 4 to 5, we have been getting this exception where we are trying to get scrollable result from named query with stateless session. so far we have tried googling it and setting parameters but it seems like cache is enabled by default in NamedQuery object and stateless session class throws java.lang.UnsupportedOperationException when trying to set chacheMode in it (hibernate's own implementation). following is the code :
public <T> Results<T> fetchScrollableResultsByQueryName(String queryName, List<String> paramNames, List<Object> paramValues){
StatelessSession slsession = getStatelessSession();
ScrollableResults scrollableResults = createQueryByName(queryName, slsession, paramNames, paramValues).scroll(ScrollMode.FORWARD_ONLY);
return(new ResultsHibernateStateless<T>(scrollableResults, slsession));
}
protected Query createQuery(String queryStr, StatelessSession slsession, List<String> paramNames, List<Object> paramValues ) {
Query query = (Query) slsession.createQuery(queryStr);
setQueryTimeout(query);
query.setFetchSize(fetchSize);
query.setReadOnly(true);
query.setCacheMode(null);
query.setCacheable(false);
this.addNamedParamToQuery(query, paramNames, paramValues);
return query;
}
and this is stacktrace of exception
2020-09-11 10:04:18,922 ERROR uk.ac.ebi.ppmc.business.service.bookindexer.BookPPMCService/processBooks 202 -
java.lang.UnsupportedOperationException
at org.hibernate.internal.StatelessSessionImpl.setCacheMode(StatelessSessionImpl.java:457)
at org.hibernate.query.internal.AbstractProducedQuery.beforeQuery(AbstractProducedQuery.java:1437)
at org.hibernate.query.internal.AbstractProducedQuery.scroll(AbstractProducedQuery.java:1485)
at org.hibernate.query.internal.AbstractProducedQuery.scroll(AbstractProducedQuery.java:110)
at uk.ac.ebi.literature.db.dao.impl.CrudDAOImpl.fetchScrollableResultsByQueryName(CrudDAOImpl.java:505)
at uk.ac.ebi.ppmc.business.service.bookindexer.BookLoaderService.loadBookInfo(BookLoaderService.java:114)
at uk.ac.ebi.ppmc.business.service.bookindexer.BookPPMCService.processBooks(BookPPMCService.java:176)
at uk.ac.ebi.ppmc.business.service.bookindexer.BookPPMCService.main(BookPPMCService.java:95)
if someone can tell if that's a known bug in hibernate 5 or is there any solution for this problem??
I think the root cause of your problem in this line:
You should just remove it, because if you look at the implementation you will see this:
See also additional information in the documentation: