问题描述
在将项目从休眠4升级到5后,我们一直在尝试使用无状态会话从命名查询中获取可滚动结果时遇到此异常。到目前为止,我们已经尝试使用谷歌搜索并设置参数,但似乎默认在NamedQuery对象中启用了缓存,并且在尝试在其中设置chacheMode时,无状态会话类抛出java.lang.UnsupportedOperationException(冬眠的自己的实现)。以下是代码:
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<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,paramValues);
return query;
}
这是异常的堆栈跟踪
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)
如果有人可以说出这是休眠5中的已知错误还是有解决此问题的解决方案?
解决方法
我认为这是您问题的根本原因:
query.setCacheMode(null);
您应该删除它,因为如果您查看implementation,将会看到以下内容:
@Override
public void setCacheMode(CacheMode cm) {
throw new UnsupportedOperationException();
}
另请参见documentation中的其他信息:
,
StatelessSession
未提供的某些内容包括:
- 一级缓存
- 与任何第二级或查询缓存的交互
- 事务性后写式或自动脏检查
尝试在调用.scroll(ScrollMode.FORWARD_ONLY)之前设置提示,例如:
query.setHint(JPA_SHARED_CACHE_STORE_MODE,null); query.setHint(JPA_SHARED_CACHE_RETRIEVE_MODE,null);