微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

对于我的应用程序,我想为几个实体使用两种不同的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时构建一次.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐