Three of the more important concepts in Hibernate are summarized in a second.

Source: Internet
Author: User

Hibernate has 2 caches

* The first level of cache is the session level cache, which is a transaction-scoped cache (Session cache)

* The second level of cache is the sessionfactory level cache, which is a process-wide cache (level two cache



First-level cache of Session

* When querying using Hibernate, the query results are placed in the first level cache of the session and objects exist in the first level cache.
The object uses the value of the OID of the property to differentiate
At this point when the same OID is used for querying, first in the session cache to find out if there is the same OID

* If the same OID exists, the database is not queried at this time, but the objects present in the first-level cache are used directly

* If the same OID is not present, then query the database, and then put the result data of the query into session level cache

Purpose: Reduce the number of times a database is accessed



Snapshot of Session

* Query the database with ID, put the results of the query into the session cache, and copy the data, and put it in the session snapshot.

* When Tr.commit () is used, the first level cache (flush) of the session is also cleared

* When the session cache is cleared, the OID is used to determine the object in the first cache and the object in the snapshot.

* If the properties in 2 objects (cached objects and snapshot objects) change, the UPDATE statement is executed, and the database is updated at this time.
Update to data in a first-level cache

* If a property in 2 objects does not change, the UPDATE statement is not executed at this time

Purpose: Ensure consistency with data in the database



Session Level Two cache

Second-level cache

A configurable cache plug-in includes class-level cache area, collection-level cache area, query-level cache area, timestamp-level cache area

The following steps are generally used:

1 copy Ehcache-1.5.0.jar to the current project's Lib directory

2 Turn on level two cache

<property name= "Hibernate.cache.use_second_level_cache" >true</property>

3 to specify the vendor for the cache

<property name= "Hibernate.cache.provider_class" >

Org.hibernate.cache.ehcacheprovider</property>

4 specifying classes that use level two caching

* Method one in using the *.hbm.xml configuration of the class

Choose a persistence class that requires a level two cache, and set its level two cache for concurrent access policies.
The cache child element of the <class> element indicates that Hibernate caches the simple properties of the object.
However, the collection properties are not cached, and if you want to cache the elements in the collection properties, you must include the <cache> child element in the <set> element

Method Two is configured in the Hibernate.cfg.xml file (recommended)

<!--specifies that classes using level Two caching are placed under maping--

<!--Configure class-level two cache--

<class-cache class= "Cn.itcast.c3p0.Customer" usage= "Read-write"/>

<class-cache class= "Cn.itcast.c3p0.Order" usage= "Read-write"/>

<!--Configure a level two cache at the collection level--

<collection-cache collection= "Cn.itcast.c3p0.Customer.orders"

Usage= "Read-write"/>

? 5 Configuring Ehcache Default configuration file ehcache.xml (fixed name) (placed under Classpath)

Three of the more important concepts in Hibernate are summarized in a second.

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.