HQL查询将使用Hibernate二级缓存吗 [英] Will HQL query use Hibernate second-level cache

查看:72
本文介绍了HQL查询将使用Hibernate二级缓存吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想澄清一些有关休眠的二级缓存的要点.需要澄清的一点是,HQL查询是否总是会命中数据库(至少是为了获取ID).

考虑我们有实体

class Customer {

    long id;  // Primary key

    String name;

    set <Address> addressList;   // One to many relationship

}

class Address{

    long id; // Primary key

    String houseName;

}

该地址的数据库表具有对客户(id)的外键引用,以支持一对多关系.

作为前提条件,我已将2级缓存启用为EHcache休眠模式.仅实体和关联设置为可缓存.未启用查询缓存.

我知道,如果我多次使用session.get()或session.load(),则只有第一个调用会向数据库发出查询,随后的查询将从2级缓存中获取数据. /p>

我的问题是

1)HQL是否将利用二级缓存. 在一个会话中,我执行了一个HQL以使用主键(id)从客户c,其中c.id =?").setParameter(1,1005)来获取对象.

如果我在不同的会话中运行了相同的HQL,则将Customer对象从2级缓存中获取,否则它将再次访问数据库.

2)考虑另一个执行from Customer as c left join fetch c.addressList的HQL,用于选择客户和关联的地址.

如果我在不同的会话中运行了相同的HQL,则将从第二级缓存中获取关联的地址,否则它将再次访问数据库.

解决方案

因为您尚未启用查询缓存,没有实体查询(JPQL或Criteria API)将使用第二级缓存.因此,两个查询都将针对数据库执行.

如果希望这些查询改为使用2级缓存,则可以启用查询缓存.

I would like to clarify some points regarding the secondary level cache of hibernate. The point to clarify is, will the HQL queries always hit the database (at least for getting ids).

Consider we have entities

class Customer {

    long id;  // Primary key

    String name;

    set <Address> addressList;   // One to many relationship

}

class Address{

    long id; // Primary key

    String houseName;

}

The database table for the Address has a foreign key reference to the Customer (id) to support one to many relationship.

As a precondition, I have enabled the level 2 cache for hibernate as EHcache. Only the entity and associations are set to be cacheable. Query caching is not enabled.

I know that if I use the session.get() or session.load() more than one time, only the first call will fire a query to the databases and subsequent ones will take the data from level 2 cache.

My questions are

1) Will HQL take advantage of the second level cache. In one session i executed one HQL to get the object using the primary key (id), "from Customer c where c.id = ? ").setParameter(1, 1005).

If I ran the same HQL in a different session, will the Customer object is taken from the level 2 cache or it will hit the database again.

2) Consider another HQL executed from Customer as c left join fetch c.addressList for selecting the customer and associated Address.

If i ran the same HQL in a different session, will the associated Address be taken from the level 2 cache or it will hit the database again.

解决方案

Because you haven't enabled the Query Cache, no entity query (JPQL or Criteria API) will use the 2nd-level cache. Therefore, both queries will be executed against the database.

You can enable the query cache if you want those queries to use the 2nd-level cache instead.

这篇关于HQL查询将使用Hibernate二级缓存吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆