1

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??

  • also we found this issue posted on hibernate community https://hibernate.atlassian.net/browse/HHH-12012 but not sure if they acknowledge it or provide any solution for it, – Zunaira Shafique Sep 11 '20 at 13:12

2 Answers2

0

I think the root cause of your problem in this line:

query.setCacheMode(null);

You should just remove it, because if you look at the implementation you will see this:

@Override
public void setCacheMode(CacheMode cm) {
    throw new UnsupportedOperationException();
}

See also additional information in the documentation:

Some of the things not provided by a StatelessSession include:

  • a first-level cache
  • interaction with any second-level or query cache
  • transactional write-behind or automatic dirty checking
SternK
  • 11,649
  • 22
  • 32
  • 46
  • well i tried both, setting different values plus removing this line altogether, the problem iscacheMode is pre initialized before this point – Zunaira Shafique Sep 11 '20 at 13:38
  • So, what exception do you see after this line removing. – SternK Sep 11 '20 at 13:39
  • same exception. – Zunaira Shafique Sep 11 '20 at 13:43
  • Could you please provide full implementation of all mentioned in this code snippet methods like : `getStatelessSession`, `setQueryTimeout`, `addNamedParamToQuery`. Or post minimal project that allow to reproduce this problem on github. – SternK Sep 11 '20 at 13:49
  • well i have attached this reported hibernate issue [link] https://hibernate.atlassian.net/browse/HHH-12012 . you can try to run testcases mentioned in this issue and will see the exception. – Zunaira Shafique Sep 11 '20 at 13:53
  • `it seems like cache is enabled by default` - Why do you think so? As it stated in the documentation: `StatelessSession` does not support any caches. See the reference to the documentation in my answer. – SternK Sep 11 '20 at 13:59
  • i know stateless session dont support any cache but NamedQuery object have cache enabled by default and when there is point where named query object try to set cache in stateless session object it throws exception. now I cant seem to find any way to set cachte mode null in NamedQuery object. – Zunaira Shafique Sep 11 '20 at 14:03
  • What exactly hibernate version do you use? Please tell the full version number? – SternK Sep 11 '20 at 14:15
  • we are using 5.4.12. – Zunaira Shafique Sep 14 '20 at 09:46
0

try to set Hint before call .scroll(ScrollMode.FORWARD_ONLY) like that:

query.setHint(JPA_SHARED_CACHE_STORE_MODE, null); query.setHint(JPA_SHARED_CACHE_RETRIEVE_MODE, null);