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

1

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

9 Comments

well i tried both, setting different values plus removing this line altogether, the problem iscacheMode is pre initialized before this point
So, what exception do you see after this line removing.
same exception.
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.
well i have attached this reported hibernate issue [link] hibernate.atlassian.net/browse/HHH-12012 . you can try to run testcases mentioned in this issue and will see the exception.
|
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.