本文共 783 字,大约阅读时间需要 2 分钟。
对于经常用到的查询语句,如果其中的查询参数是固定的,则可以将这样的查询结果也存储到Hibernate的二级缓存中。 应用中设置了查询缓存后,查询的结果集将被存储到缓存中,但并非缓存结果集中具体的entity对象,而是只缓存entity对象的标识符。另外,查询缓存往往代价很大,如果缓存的查询不当,性能有很大影响。 1. 开启查询缓存的步骤: 1).配置文件中,设置配置参数hibernate.cache.use_query_cache=true 2).应用代码中,调用对应的方法Listpersons = session.createQuery( "select p " + "from Person p " + "where p.name = :name").setParameter( "name", "John Doe").setCacheable(true).list();
注意其中的setCacheable(true)方法
List注意其中的setHint( "org.hibernate.cacheable", "true")方法。 2. 清空查询缓存 SessionFactory.evictQueries()persons = entityManager.createQuery( "select p " + "from Person p " + "where p.name = :name", Person.class).setParameter( "name", "John Doe").setHint( "org.hibernate.cacheable", "true").getResultList();
转载地址:http://nslai.baihongyu.com/