本文介绍了如何禁用Hibernate的一些某些实体的二级缓存,而​​不改变注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Hibernate的二级缓存在我的应用程序,对于某些商业原因的我无法改变实体标注任何更多

I'm using Hibernate second level cache in my application, for certain business reason I can't change the entity annotation any more.

在我的项目中,除了改变从休眠数据库,还存在其他本地SQL不通过Hibernate去。因此,从数据库的本地SQL更新后Hibernate的二级缓存的数据可能是过时的。这就是为什么我想要的禁用某些实体二级缓存(编程方式或其他方式比改变注释)。

In my project, apart from changing the Database from Hibernate, there exist also other native SQL that do not go through Hibernate. Therefore, the Hibernate second-level cache data could be stale after database being updated from native SQL. That's why I want to disable the second-level cache for certain entities (programmatically or other way than changing annotation).

在此先感谢!

推荐答案

警告:由于延Schauder不说,这是不可能配置的Ehcache在内存中存储0个元素设置maxElementsInMemory =0因为它能有效地引起相反的效果 - 设置无限大小的缓存。这种行为是不是在页提到,但在案的页。

WARNING: As Jens Schauder noted, it is impossible to configure Ehcache to store 0 elements in memory by setting maxElementsInMemory="0" as it effectively causes opposite effect - sets unlimited size for the cache. This behaviour is not mentioned on the Hibernate Caching page but is documented on Cache Configuration page.

我很快审议了有关文件,并没有发现可供选择的方法呢。我不能由我自己来删除这个答案。 : - (

I have quickly reviewed the documentation and haven't found alternative approach yet. I am unable to delete this answer by myself. :-(

我原来的建议是:

您可以配置二级缓存的实施提供短TTL时间和/或存储特定实体类型的0项。

You can configure the implementation provider of second level cache to short TTL times and/or to store 0 entries of particular entity type.

例如。如果您正在使用的Ehcache,你可以在ehcache.xml中配置它:*

E.g. if you are using the Ehcache, you can configure it in ehcache.xml:*

<cache
name="com.problematic.cache.EntityName"
maxElementsInMemory="0" <<== this should effectively disable caching for EntityName
overflowToDisk="false" <<== Do not overflow any entries to disk
/>

请参阅的Ehcache文档中

See Hibernate Caching in Ehcache documentation.

这篇关于如何禁用Hibernate的一些某些实体的二级缓存,而​​不改变注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 06:34