博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hibernate中的Query cache(查询缓存)
阅读量:4177 次
发布时间:2019-05-26

本文共 783 字,大约阅读时间需要 2 分钟。

对于经常用到的查询语句,如果其中的查询参数是固定的,则可以将这样的查询结果也存储到Hibernate的二级缓存中。
应用中设置了查询缓存后,查询的结果集将被存储到缓存中,但并非缓存结果集中具体的entity对象,而是只缓存entity对象的标识符。另外,查询缓存往往代价很大,如果缓存的查询不当,性能有很大影响。
1. 开启查询缓存的步骤:
1).配置文件中,设置配置参数hibernate.cache.use_query_cache=true
2).应用代码中,调用对应的方法

  • Hibernate API:

List
persons = session.createQuery( "select p " + "from Person p " + "where p.name = :name").setParameter( "name", "John Doe").setCacheable(true).list();

注意其中的setCacheable(true)方法

  • JPA API:

List
persons = entityManager.createQuery( "select p " + "from Person p " + "where p.name = :name", Person.class).setParameter( "name", "John Doe").setHint( "org.hibernate.cacheable", "true").getResultList();
注意其中的setHint( "org.hibernate.cacheable", "true")方法。

2. 清空查询缓存
SessionFactory.evictQueries()

转载地址:http://nslai.baihongyu.com/

你可能感兴趣的文章
linux驱动实例
查看>>
android ArrayList<String> 转 String[]
查看>>
RecyclerView baseadapter
查看>>
Android中应用程序如何获得系统签名权限
查看>>
MPAndroidChart 动态更新
查看>>
Recycler表格(excelPanel)
查看>>
android一行代码实现沉浸式布局效果
查看>>
json, recyclerView问题
查看>>
cmake处理多源文件目录的方法
查看>>
Service Intent must be explicit
查看>>
android studio SDK开发
查看>>
studio 统计代码的行数
查看>>
字符数组和16进制互换
查看>>
PHP项目中出现致命错误: Class 'Redis' not found
查看>>
There is no tracking information for the current branch.
查看>>
fatal: refusing to merge unrelated histories
查看>>
Git命令还原未提交的变更
查看>>
Linux系统中环境变量的配置
查看>>
Linux系统中配置脚本程序开机启动
查看>>
让Linux系统上的nginx支持php程序
查看>>