Hibernate Second level Cache <>

后端 未结 2 1317
迷失自我
迷失自我 2021-02-04 21:10

I want use second level cache in my hibernate Project but I just know a little about hibernate second level cache, can any one explain how shoud I use this in my code and what c

2条回答
  •  旧巷少年郎
    2021-02-04 21:52

    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 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.

提交回复
热议问题