"11.0" Hibernate cache mechanism (level and level two cache)

Source: Internet
Author: User

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


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" />


<property name="Author" type="java.lang.String">

<column name="Author" />


<property name= "name" type="java.lang.String">

<column name="name" />


<property name= "Price" type="java.lang.Double">

<column name="Price" precision=" $" scale="0" not-null="true" />


<property name="pubDate" type="Java.sql.Timestamp">

<column name="pubDate" length=" />"



In the configuration file

<class-cache usage="read-only" class="Cn.siggy.pojo.Book"/>

f) Use:


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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.