use hibernate second level cache

  Hibernate comes with three different caches: first level, second level and query cache.
  The first level cache is the Hibernate Session and is used to track the state of entities during the current Session (or unit of work). This is a transaction-level cache.
  The second level cache shares entity state across various Session. This is a SessionFactory-level cache.
The query cache is used to cache queries (and their parameters) and their results.


steps to use cache
add ehcache dependency, enable following in conf
[code=properties]
hibernate.cache.use_query_cache=true
hibernate.cache.use_second_level_cache=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
## If the cache is used in a JTA environment, you must specify the property
## Hibernate.transaction.manager_lookup_class and naming a strategy for obtaining the JTA TransactionManager.
hibernate.transaction.manager_lookup_class=com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup


put following tag in entities hibernate mapping file

<!--details see office document-->
<cache usage="transactional|read-write|nonstrict-read-write|read-only" />
<set>
    <cache usage="transactional|read-write|nonstrict-read-write|read-only" />
</set>




The Second Level Cache in the documentation
Hibernate: Truly Understanding the Second-Level and Query Caches

猜你喜欢

转载自lingceng.iteye.com/blog/1687902