简体   繁体   English

在orm.xml中使用Hibernate二级缓存?

[英]Hibernate second-level cache in orm.xml?

Having been googling for hours, I realize that users can use either xml file(orm.xml, I suppose?) or annotations in JPA, or both of them at the same time. 谷歌搜索了几个小时,我意识到用户可以使用xml文件(orm.xml,我猜?)或JPA中的注释,或同时使用它们两者。 I'm i correct? 我是对的? So, My project use the second-level cache, which is not in the JPA specification. 因此,我的项目使用二级缓存,这不在JPA规范中。 And I use annotations like: @org.hibernate.annotations.Cache(usage = org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE ) 我使用的注释如下: @ org.hibernate.annotations.Cache(usage = org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE)

for each entities. 对于每个实体。 However, I guess that the annotation above doesn't belongs to be JPA annotation, therefore, I don't know if there is anything equivalent in orm.xml? 但是,我猜上面的注释不属于JPA注释,因此,我不知道orm.xml中是否有任何等价物? I have had a look at the xml schema of orm.xml, and there is not such hibernate element permitted in the schema. 我看了一下orm.xml的xml架构,架构中没有允许这样的hibernate元素。

If I can't do the second-level cache config in the orm.xml, is there any way other than in the annotation? 如果我不能在orm.xml中执行二级缓存配置,除了注释之外还有其他方法吗? The reason that I don't want to do it the annotation is because I have two applications, one use READ_WRITE concurrency strategy and another use READ_ONLY concurrency strategy. 我之所以不想这样做是因为我有两个应用程序,一个使用READ_WRITE并发策略,另一个使用READ_ONLY并发策略。

Thanks in advance!!! 提前致谢!!!

Kewei 科威

您可以使用hibernate.cfg.xml,hibernate.properties或* .hbm.xml文件打开具有不同缓存策略的特定对象的缓存

If you don't want to use annotations, then check the documentation of your hibernate second level cache implementation (aka "cache provider", for example, ehcache ) how to configure it. 如果您不想使用注释,请查看hibernate二级缓存实现(也称为“缓存提供程序”,例如, ehcache )的文档,了解如何配置它。

Note that Hibernate allows to use different cache providers, so there is no general answer to your question. 请注意,Hibernate允许使用不同的缓存提供程序,因此您的问题没有一般的答案。

因此,我认为我的问题的当前解决方案是用Hibernate映射文件替换每个实体的所有注释,以便对于不同的部署(应用程序),我们可以使用不同的Hibernate映射文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM