尝试从缓存中获取数据(对象列表)时,Hibernate二级缓存+ Ehcache问题

时间:2012-03-08 13:28:39

标签: hibernate caching ehcache

我可以通过HibernateTemplate.load(Class entityClass, Serializable id)

从高速缓存中获取数据,但只能获取一个对象的数据
[2012-03-06 04:34:18,755] [INFO] [com.uprr.app.rim.components.common.service.RIMSpringApplicationContext] [ApplicationContext is set and ready for use for XMF service clients....]
[2012-03-06 04:34:20,255] [WARN] [org.hibernate.mapping.RootClass] [composite-id class does not override equals(): com.uprr.app.rim.components.exinterface.domain.Track]
[2012-03-06 04:34:20,270] [WARN] [org.hibernate.mapping.RootClass] [composite-id class does not override hashCode(): com.uprr.app.rim.components.exinterface.domain.Track]
[2012-03-06 04:34:35,442] [DEBUG] [org.hibernate.cache.CacheFactory] [instantiating cache region: com.uprr.app.rim.components.masters.domain.CommPlaceTypeDO usage strategy: read-only]
[2012-03-06 04:34:35,442] [WARN] [org.hibernate.cache.CacheFactory] [read-only cache configured for mutable class: com.uprr.app.rim.components.masters.domain.CommPlaceTypeDO]
[2012-03-06 04:34:35,646] [DEBUG] [org.hibernate.cache.CacheFactory] [instantiating cache region: com.uprr.app.rim.components.masters.domain.CommPlaceDO usage strategy: read-only]
[2012-03-06 04:34:35,646] [WARN] [org.hibernate.cache.CacheFactory] [read-only cache configured for mutable class: com.uprr.app.rim.components.masters.domain.CommPlaceDO]
[2012-03-06 04:34:36,005] [INFO] [org.hibernate.cache.UpdateTimestampsCache] [starting update timestamps cache at region: org.hibernate.cache.UpdateTimestampsCache]
[2012-03-06 04:34:36,021] [INFO] [org.hibernate.cache.StandardQueryCache] [starting query cache at region: org.hibernate.cache.StandardQueryCache]
[2012-03-06 04:34:36,380] [DEBUG] [org.hibernate.SQL] [select commplacet0_.COM_PLAC_TYPE_ID as COM1_82_, commplacet0_.COM_PLAC_TYPE_CODE as COM2_82_, commplacet0_.COM_PLAC_TYPE_DESC as COM3_82_, commplacet0_.ACT_FLAG as ACT4_82_, commplacet0_.CRTN_USER_ID as CRTN5_82_, commplacet0_.CRTN_DT as CRTN6_82_, commplacet0_.LAST_UPTD_USER_ID as LAST7_82_, commplacet0_.LAST_UPTD_DT as LAST8_82_ from RIM_COM_PLAC_TYPE_MSTR commplacet0_ where commplacet0_.COM_PLAC_TYPE_ID=172 order by commplacet0_.COM_PLAC_TYPE_DESC]
[2012-03-06 04:34:37,052] [DEBUG] [org.hibernate.cache.ReadOnlyCache] [**Caching**: com.uprr.app.rim.components.masters.domain.CommPlaceTypeDO#172]
[2012-03-06 04:34:37,130] [DEBUG] [org.hibernate.cache.ReadOnlyCache] [**Cache hit:** com.uprr.app.rim.components.masters.domain.CommPlaceTypeDO#172]

但当我尝试使用loadAll(),getHibernateTemplate().find(strQuery)Query.list()从缓存中获取对象列表时...这些方法将再次命中数据库并尝试再次缓存数据并在记录器中显示警告:

[2012-03-08 06:56:10,050] [DEBUG] [org.hibernate.cache.ReadOnlyCache] [**item already cached:** com.uprr.app.rim.components.masters.domain.CommPlaceTypeDO#**180**]
[2012-03-08 06:56:10,050] [DEBUG] [org.hibernate.cache.ReadOnlyCache] [item already cached: com.uprr.app.rim.components.masters.domain.CommPlaceTypeDO#**169**]
[2012-03-08 06:56:10,050] [DEBUG] [org.hibernate.cache.ReadOnlyCache] [item already cached: com.uprr.app.rim.components.masters.domain.CommPlaceTypeDO#**166**]
[2012-03-08 06:56:10,050] [DEBUG] [org.hibernate.cache.ReadOnlyCache] [item already cached: com.uprr.app.rim.components.masters.domain.CommPlaceTypeDO#**139**]
[2012-03-08 06:56:10,050] [DEBUG] [org.hibernate.cache.ReadOnlyCache] [item already cached: com.uprr.app.rim.components.masters.domain.CommPlaceTypeDO#140]
[2012-03-08 06:56:10,050] [DEBUG] [org.hibernate.cache.ReadOnlyCache] [item already cached: com.uprr.app.rim.components.masters.domain.CommPlaceTypeDO#138]
[2012-03-08 06:56:10,050] [DEBUG] [org.hibernate.cache.ReadOnlyCache] [item already cached: com.uprr.app.rim.components.masters.domain.CommPlaceTypeDO#149]
[2012-03-08 06:56:10,050] [DEBUG] [org.hibernate.cache.ReadOnlyCache] [item already cached: com.uprr.app.rim.components.masters.domain.CommPlaceTypeDO#143]
[2012-03-08 06:56:10,050] [DEBUG] [org.hibernate.cache.ReadOnlyCache] [item already cached: com.uprr.app.rim.components.masters.domain.CommPlaceTypeDO#130]
[2012-03-08 06:56:10,050] [DEBUG] [org.hibernate.cache.ReadOnlyCache] [item already cached: com.uprr.app.rim.components.masters.domain.CommPlaceTypeDO#142]
[2012-03-08 06:56:10,050] [DEBUG] [org.hibernate.cache.ReadOnlyCache] [item already cached: com.uprr.app.rim.components.masters.domain.CommPlaceTypeDO#165]
[2012-03-08 06:56:10,050] [DEBUG] [org.hibernate.cache.ReadOnlyCache] [item already cached: com.uprr.app.rim.components.masters.domain.CommPlaceTypeDO#167]
[2012-03-08 06:56:10,050] [DEBUG] [org.hibernate.cache.ReadOnlyCache] [item already cached: com.uprr.app.rim.components.masters.domain.CommPlaceTypeDO#168]

1 个答案:

答案 0 :(得分:4)

在激活二级缓存时,我实际上遇到了同样的问题。试图缓存我的标准。

如果我没有将cacheable设置为true,

更新:我终于找到了解决方案,问题来自我的hibernate版本。我在休眠3.2。升级到3.6.9解决了我的问题:)

希望它可以提供帮助!