开发者

Hiber cache : Cache all fixed data permanently

I have few tables like Country,state city which has static data. User do开发者_JS百科 not enter any data in this data. I create pojo for Country, State, City. There are few pojo which has mapping with static data. My requirement is that whenever any request comes to Hibernate for Country (21), it do not hit database but return data from cache . Is this possible in Hibernate. I need few pointers and your views to implement caching in my project. I am using hibernate annotations.


My requirement is that whenever any request comes to Hibernate for Country (21), it do not hit database but return data from cache. Is this possible in Hibernate.

Yes, this is possible using the Second Level Cache and this kind of Entities (read-only) are the perfect candidates for caching (they are the easiest to manage). You'll need to:

  • enable the 2nd level cache
    • set the hibernate.cache.use_second_level_cache property to true in your configuration
  • choose a 2nd level cache provider (I suggest EHCache)
    • set the hibernate.cache.provider_class property accordingly
  • mark your entities as cacheable (using the read-only strategy)
    • Add @Cache(usage = CacheConcurrencyStrategy.READ_ONLY) to your entities

References

  • Hibernate Core Reference Guide
    • 3.4.4. Second-level and query cache
    • 19.2. The Second Level Cache
  • Hibernate Annotations 3.4 Reference Guide
    • 2.4.8. Cache

More Resources

  • Hibernate: Truly Understanding the Second-Level and Query Caches
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜