【问题标题】:Hibernate different caches for different tables?为不同的表休眠不同的缓存?
【发布时间】:2015-04-24 11:49:20
【问题描述】:

在我的项目中,我必须根据使用情况将不同的表数据放到不同的 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 中的缓存名称)?

有没有办法像 infinispan 拦截器一样拦截休眠 L2 缓存调用?

【问题讨论】:

    标签: java hibernate infinispan second-level-cache


    【解决方案1】:

    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>
    

    【讨论】:

    • 确实,这是实现这一目标的最简单方法。请记住,在 Wildfly 或 EAP 中运行时,属性名称可能会有所不同。见文档。
    • 如果com.acme.Person 是子类并且相关的hbm 是,这是否有效。在我的情况下,我只需要在 java 继承树中缓存一个对象,但所有这棵树只有一个表。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-23
    • 2010-11-26
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多