hibernate,hql与sql的缓存使用
2014-04-14 17:57
381 查看
getSessionFactory().evictQueries(); //清除缓存 1.在hibernate中createQuery执行hql查询的时候使用查询缓存: getSession().createQuery(hql).setCacheable(true).setCacheRegion("user").list(); 2.在hibernate中createSQLQuery执行sql时使用查询缓存: getSession().createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(User.class)).setCacheable(true).setCacheRegion("user").list(); 这样会报错,原因是That's because the aim of the second level cache is to (ultimately) cache entities that Hib knows about. I haven't verified this but I'm not surprised that caching sql query doesn't work as those are not (directly) translated by hibernate into entities. 解决方法: It's solutioned adding column's types to the query, using the method "addScalar()" like this: getSession().createSQLQuery(sql).addScalar("id", IntegerType.INSTANCE) .addScalar("name",StringType.INSTANCE) .addScalar("age",IntegerType.INSTANCE).setResultTransformer(Transformers.aliasToBean(User.class)) .setCacheable(true).setCacheRegion("user").list(); 转自:http://www.360doc.com/content/14/0331/13/16589843_365170644.shtml |
相关文章推荐
- Ubuntu下安装配置MySQL及MySQL++
- Oracle 创建 、启用、停用job、修改job 间隔
- ubuntu install mysql
- [原创实践]RedHat Enterprise Linux 5 安装GCC和redis
- Oracle SQL 语句
- ubuntu 安装memcache扩展
- windows系统 安装mysql5.5图解
- SQL数据库超时的原因与解决方法
- Oracle建立DBLink
- MongoDB的内部构造 From 《MongoDB The Definitive Guide》
- 往mysql中插入数据库发生版本错误的信息
- 收缩数据库
- oracle ipad函数(从左边填充)
- 小觑数据库(SqlServer)查询语句执行过程
- Oracle数据库RAC集群管理-CRS相关2(crsctl)
- SQL 获取一张表列的总数
- Oracle数据库RAC集群管理-CRS相关1(crs_stat)
- 数据库表格所占空间及索引所占空间情况
- Red Hat Linux5.5 下安装oracle10g
- Mac下安装MySQLdb模块