对于我的应用程序,我想对几个实体使用两种不同的 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)只支持四种典型的缓存策略:
Hibernate 还允许您设置默认缓存策略:
hibernate.cache.default_cache_concurrency_strategy=read-write
并且您可以使用实体级注释或特定于 Hibernate 的 XML 配置来覆盖它。
无论如何,您不能在运行时使用条件缓存策略,因为实体缓存策略仅在 SessionFactory
启动时构建一次。
关于java - 如何对几个实体使用两种不同的 Hibernate 缓存策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31726987/