为不同的表Hibernate不同的缓存?

时间:2015-02-23 07:43:40

标签: java hibernate infinispan second-level-cache

在我的项目中,我必须根据使用情况将不同的表数据放到不同的L2缓存中。某些表需要复制缓存,而其他表只能提供分布式缓存。 hibernate可以实现这种配置吗?

在hibernate.properties中,我们只能指定一个缓存

 hibernate.cache.infinispan.entity.cfg=replicated-cache-entity
 hibernate.cache.infinispan.collection.cfg=replicated-cache-entity

此缓存在ininispan.xml中配置

   <namedCache name="replicated-cache-entity">
       <clustering mode="replication">
           <stateRetrieval fetchInMemoryState="false" timeout="20000"/>
           <sync replTimeout="20000"/>
       </clustering>
   </namedCache>

有没有办法为单个表或类指定不同的缓存(infinispan中的缓存名称)?

OR

有没有办法拦截hibnate L2缓存调用,如infinispan拦截器?

1 个答案:

答案 0 :(得分:3)

Infinispan user guide还提到了每个实体的缓存配置:

  

最重要的是,这种更细粒度的缓存定义使用户能够   基于每个实体/集合定义缓存设置。例如:

<!-- If using Hibernate, add to your hibernate.cfg.xml --> 
<property name="hibernate.cache.infinispan.com.acme.Person.cfg">   
  person-entity
</property> 
<property name="hibernate.cache.infinispan.com.acme.Person.addresses.cfg">   
  addresses-collection
</property>
相关问题