加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

java – 如何为几个实体使用两种不同的Hibernate缓存策略

发布时间:2020-12-15 01:32:46 所属栏目:大数据 来源:网络整理
导读:对于我的应用程序,我想为几个实体使用两种不同的hibernate缓存策略.因此(afaik,请纠正我,如果我错了)在实体上使用注释 @Cache(usage=ConditionalStrategy)public class MyEntity{...} 不会起作用,因为“ConditionalStrategy”必须是一个常量字段(为了与注释

对于我的应用程序,我想为几个实体使用两种不同的hibernate缓存策略.因此(afaik,请纠正我,如果我错了)在实体上使用注释

@Cache(usage=ConditionalStrategy)
public class MyEntity{
...
}

不会起作用,因为“ConditionalStrategy”必须是一个常量字段(为了与注释一起使用).

我已经了解了如何使用hibernate.cfg文件为每个实体配置缓存策略
(见https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/performance.html#performance-cache-mapping)
但我想使用Spring LocalContainerEntityManagerFactoryBean的JPA属性直接设置它们,即

LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
Properties jpaProps = new Properties();
// what to put here to configure the caching strategies per entity?
jpaProps.put(...,....) 
factory.setJpaProperties(jpaProbs);

我如何设置JPA属性以复制基于注释的配置?这甚至可能吗?

对于那些面临同样问题的人来说更新:如果有人遇到同样的问题,还要考虑使用Spring Cache(http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html)抽象而不是hibernate注释(这是我最后所做的)

最佳答案
首先,使用ConditionalStrategy是没有意义的,因为实际的缓存提供程序(例如Ehcache)仅支持四种典型的缓存策略:

> read-only
> nonstrict-read-write
> read-write
> transactional

Hibernate还允许您设置默认缓存策略:

hibernate.cache.default_cache_concurrency_strategy=read-write

您可以使用实体级注释或Hibernate特定的XML配置覆盖它.

无论如何,您不能在运行时使用条件缓存策略,因为实体缓存策略仅在启动SessionFactory时构建一次.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读