(1) The cache is to save objects previously queried from the database and used in memory (a data structure), this data structure is usually or similar to HashMap, when you want to use an object in the future, the first query in the cache whether there is this object, if there is to use the object in the cache, If not, query the database and save the queried object in the cache for the next use.
2) Hibernate session is a kind of cache, we usually call it hibernate cache, when you want to use the session from the database to query an object, the session is to see whether the object exists from within itself, the existence is directly returned, Does not exist to access the database and save the query's results inside itself. Since the session represents a conversation process, and the sessions are associated with a database connection, the session is best not to remain open for long periods of time, usually only for one transaction, and should be closed at the end of the transaction. And the session is thread insecure, and being shared by multiple threads is prone to problems. Usually only the global sense of the cache is the real cache application, there is a large cache value, therefore, hibernate session of this cache cache role is not obvious, the application is not very valuable. Hibernate's Level Two cache is a global cache configured for Hibernate, allowing multiple threads and multiple transactions to share this cache. We want a person to use it, others can use it, and the session doesn't have that effect.
(3) Level two caches are software parts that are independent of hibernate, products that belong to third parties, and multiple vendors and organizations provide caching products, such as Ehcache and Oscache, and so on. Using a level two cache in Hibernate, The first step is to configure which manufacturer's cache product to use in the Hibernate.cfg.xml configuration file, then configure the cache product's own configuration file, and finally configure which entity objects in Hibernate are to be included in the management of level two cache. Understanding the two-level caching principle and with this idea, it is easy to configure Hibernate's level two cache. Extended knowledge: A sessionfactory can be associated with a level two cache, or a level two cache can only be responsible for caching data in a database, when using Hibernate's Level two cache, Be careful not to have other apps or sessionfactory to change the data in the current database, so that the cached data will not be consistent with the actual data in the database.
Hibernate's Cache