据我了解,当使用主键加载对象时,将使用二级缓存。这包括获取关联。我只能想到上面的 session.get()、session.load 方法,其中二级缓存将出现在图片中。
如果关联是集合或其他一些实体,如何缓存它?例如:-
@Cacheable
public class Department{
private List Employees;
private DepatmentDetail detail ;
}
如何使关联员工和详细信息可缓存?我想我需要在关联员工和细节上面提到@cache。 但这不起作用?
当开发人员执行 department.getEmployees() 时,hibernate 将在内部触发查询,即
select * from employees where deptId =1;
现在,如果我在明确进行上述查询并获取结果的地方使用查询缓存,查询将再次触发到数据库。为什么再次触发查询。我认为这与hibernate内部如何存储二级缓存和查询缓存的结果有关(它们可能存储在不同的区域)。如果有人也能阐明这方面,那就太好了。
最佳答案
查看下面的链接,其中有详细解释。
查询级缓存:
Hibernate also implements a cache for query resultsets that integrates closely with the second-level cache.
This is an optional feature and requires two additional physical cache regions that hold the cached query results and the timestamps when a table was last updated. This is only useful for queries that are run frequently with the same parameters.
二级缓存
Hibernate is compatible with several second-level cache providers. Any implementation can be used for second level cache.
区别:
Query Cache 的唯一目的是缓存查询,而 Second Cache 也可用于缓存其他缓存。
查询缓存由 Hibernate 内部提供,而对于二级缓存,您必须选择一些外部二级缓存,例如 Infinispan、EHCache 等。
关于java - 二级缓存 vs 查询缓存 vs 集合缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23454133/