Hibernate Second level Cache <<net.sf.ehcache.hibernate.EhCacheProvider>>

血红的双手。 提交于 2019-12-03 00:46:00

The annotation you're looking for is org.hibernate.annotations.Cache. Basic usage is:

@Entity
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public MyEntity {
    ...

  @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
  public List<ElementType> getSomeCollection() {
    ...
  }
}

For queries, you need to enable query cache by setting hibernate.cache.use_query_cache property to true AND specify that query is cacheable in its declaration (for named queries) or by calling setCacheable(true) on query instance.

All that said, you need to be really careful with caching and REALLY UNDERSTAND what you're doing, otherwise it'll do more harm than help. Don't look at it as "quick fix" - caching everything, for example, is definitely the wrong thing to do.

Your settings will make the second-level and query caches available for use in your project, but you still need to enable it for specific entities, collections, and queries. This requires some careful planning because there are trade-offs that you'll need to understand. In general, the second-level and query caches are appropriate for read-only or read-mostly data, but not volatile data. If you don't already own it, I would recommend picking up a copy of Java Persistence with Hibernate. It has a very good treatment of the subject.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!