1, level cache, level two cache, query cache;
2, a first-level cache is also called session cache. Life cycle is the same. The cycle is relatively short. Transaction-level caching.
Get uses a first-level cache, and when you check data with GET, first check the cache for that data, if you have the data directly from the cache, if you do not query the database, and put the data into the cache.
Load also supports first-level caching. Load also supports lazy. When load queries the data from the database, the data is also put into the cache.
3. The unique/list query does not go to the cache, but the entity object of the list query is placed in the cache.
4, iterate will execute the query ID operation, when querying the object, will check whether the cache exists. Fetch the data from the cache if it exists. Iterate objects that are queried are also put into the cache.
5. Management level cache: Flush (), clear (), evict ()
6, Level two cache: sessionfactory; process-level caching. Cluster support.
Steps to use:
A) Turn on level two cache in Hibernate.cfg.xml (default on)
b)
Configurationcache.provider_class
<!--use level two cache 3.x-- <property name="Cache.use_second_level_cache">true</property> <property name="Cache.provider_class">org.hibernate.cache.EhCacheProvider</property> <!--using level two cache 4.3--> <property name= "Cache.use_second_level_cache">true</property> <property name="Cache.region.factory_class"> Org.hibernate.cache.ehcache.ehcacheregionfactory</property> |
c) Import Ehcache.jar
D) put the ehcache.xml into SRC (etc under the search)
e) specified in the class or (Hibernate.cfg.xml)
<class name="Cn.siggy.pojo.Book" table="book" catalog="Hibernate4" > <cache usage="read-only"/> <id name="id" type="Java.lang.Integer"> <column name="id" /> <generator class="Identity" /> </id> <property name="Author" type="java.lang.String"> <column name="Author" /> </property> <property name= "name" type="java.lang.String"> <column name="name" /> </property> <property name= "Price" type="java.lang.Double"> <column name="Price" precision=" $" scale="0" not-null="true" /> </property> <property name="pubDate" type="Java.sql.Timestamp"> <column name="pubDate" length=" />" </property> </class> |
In the configuration file
<class-cache usage="read-only" class="Cn.siggy.pojo.Book"/> |
f) Use:
@Test Public void Testget () { Session session = Hibernateutil. getsession (); Transaction tx = Session.begintransaction (); Book book = [book] session.get (book). Class, 1); Emit SQL statement fetch data System. out. println (Book.getname ()); Hibernateutil. closesession (); Session = Hibernateutil. getsession (); System. out. println ("---------"); Book = [book] session.get (book). Class, 1); System. out. println (Book.getname ()); Tx.commit (); Hibernateutil. closesession (); } |
7. Query cache: Based on the level two cache.
A) Configure in Hibernate.cfg.xml
<!--configuration using query caching-- <property name="Cache.use_query_cache">true</property> |
b) Use:
Note: If you do not understand, please refer to the blog :http://blog.csdn.net/an_2016/article/details/52088712
"11.0" Hibernate cache mechanism (level and level two cache)