Hibernate provides a level two cache, and the first level of cache is Session -level caching, which is a transaction-scoped cache. This level of caching  is managed by hibernate, and generally does not require intervention; the second level of caching is the sessionfactory level of caching. It is a process-wide or cluster-wide cache. This level of caching can be configured and changed, and can be dynamically loaded and unloaded.  Hibernate also provides a query cache for query results, which relies on a second-level cache. 
1.comparison of primary and level two caches: first-level cache the second-level cache holds the data in the form of a scoped transaction scope for the bulk data cache of the persisted object object that is associated with each transaction, each with a separate first-level cache process scope or cluster scope, and the cache is shared by all transactions within the same process or cluster scope Concurrent access policy because each transaction has a separate first-level cache, no concurrency problems, no concurrency access policy because multiple transactions access the same data in the second-level cache at the same time, the appropriate concurrency access policy must be provided to ensure that a specific transaction isolation level data expiration policy does not provide a data expiration policy. Objects in the first-level cache never expire unless the application explicitly empties the cache or clears a specific object must provide a data expiration policy, such as the maximum number of objects in the memory-based cache, the longest time allowed for the object to be in the cache, and the maximum idle time allowed for the object to be in the cache Physical storage media memory memory and hard disk. The bulk data of an object is first stored in an intrinsic cache, and when the number of objects in memory reaches the limit specified in the data expiration policy, the remaining objects are written to the hard disk-based cache. The cached software is implemented inHibernateof theSessionImplementation of the cache is provided by a third party,Hibernateonly the cache adapter is available(Cacheprovider). Used to integrate specific cache plugins intoHibernatethe. How caching is enabled as long as the application passesSessioninterface to perform operations that save, update, delete, load, and query database data.HibernateThe first level cache is enabled, the data in the database is copied to the cache as objects, for bulk updates and bulk delete operations, and if you do not want to enable first level caching, you can bypassHibernate API, directly throughJDBCAPIto perform the operation of the finger. Users can configure a second-level cache on the granularity of a single collection of classes or classes. If an instance of a class is read frequently but is seldom modified, consider using a second-level cache. Only a second level cache is configured for a class or collection.Hibernateits instance is added to the second-level cache at run time. How the user manages the cache the first-level cache of physical media is memory, and due to limited memory capacity, the number of loaded objects must be limited by appropriate retrieval policies and retrieval methods. Sessionof theEvit ()method can explicitly empty a specific object in the cache, but this method is not recommended. The second-level cache of physical media can be memory and hard disk, so the second-level cache can hold a large amount of data, the data expiration policymaxelementsinmemorythe property value can control the number of objects in memory. Managing a second-level cache consists of two main areas: Select the persistence class that needs to use the second level cache, set the appropriate concurrency access policy: Select the cache adapter, and set the appropriate data expiration policy. 
    2.First -level cache management: When an application callsSessionof theSave (),Update (),savaeorupdate (),get ()orload (), and a query interface that invokes thelist (),Iterate ()orfilter ()method, if theSessionthe corresponding object does not exist in the cache .HibernateThe object is added to the first-level cache. When you clean up the cache,HibernateThe database is updated synchronously based on the state of the objects in the cache. Sessionprovides two ways to manage the cache for your application:evict (Object obj): Clears the persisted object specified by the parameter from the cache. Clear (): Empties all persisted objects in the cache. 
  3. Management of Level two cache:
  The general process for 3.1. Hibernate 's Level two cache strategy is as follows:
  1) When a condition is queried, always issue a SELECT * FROM table_name where   ... .  (select all fields) such a SQL statement to query the database and get all the data objects at once. 
  2) put all the obtained data Objects into the second level cache based on the ID . 
  3) when Hibernate accesses data Objects based on their ID , the first cache from Session level is checked ; not found, if a level two cache is configured, then from the level two cache, check, and then query the database, the results are placed in the cache by ID . 
  4) update the cache while deleting, updating, and adding data. 
  Hibernate level two cache policy, which is based on the ID the cached policy of the query has no effect on the conditional query. To do this,Hibernate provides query Cache for conditional queries . 
  3.2. What data is suitable for storage in the second level cache? 
  1 data that is rarely modified
2 data that is not very important, allowing occasional concurrent data to occur
  3 data that will not be accessed concurrently
  4 Reference data , refers to the supply of constant data reference, its limited number of instances, its instances will be referenced by many other instances of the class, the instance is rarely or never modified. 
  3.3. data that is not suitable for storage to a second level cache? 
1 data that is often modified
  2 financial data, never allow concurrency
3 data that is shared with other apps. 
  3.4. Common Cache plug -in hibernater 's Level two cache is a plug-in, and here are a few common cache plugins:
  EhCache : Can be a process-wide cache, the physical media that holds the data can be memory or hard disk, Hibernate provides support for query caching. 
  Oscache : Available as a process-wide cache, the physical media that holds the data can be either memory or hard disk, providing a rich cache data expiration policy for Hibernate provides support for query caching. 
  Swarmcache : Can be used as a cluster-wide cache, but does not support Hibernate the query cache. 
  JBossCache : Can be used as a cluster-wide cache, support transactional concurrency access policies, Hibernate provides support for query caching. 
  3.5. The main steps for configuring level two caching:
  1) Select the persistence class that requires a level two cache, and set its concurrent access policy for the named cache. This is the most serious step to consider. 
  2) Select the appropriate cache plug-in, and then edit the plugin's configuration file. 
Hibernate How to manage caching