Hibernate and snapshots

Source: Internet
Author: User

Hibernate we have been learning for four days, the knowledge we have finished, tomorrow's holiday, the day after day will end the study of hibernate, a little out of it, want to hibernate is not so difficult to say, The three more important concepts in Hibernate are summarized in a second.

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 using hibernate to query, the query results placed in the session of the first-level cache, in the first-level cache of objects, objects using the value of the OID property to differentiate, at this time using the same OID to query the time, The first is to find out if the same OID is present in the session-level cache

* 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, execute the UPDATE statement, update the database at this time, update the data in the 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

Select a persistence class that requires a level two cache, set its two-level cache concurrency access policy, and the cache child element of the <class> element indicates that Hibernate caches the simple properties of the object, but does not cache the collection properties, and if you want to cache the elements in the collection properties, you must <s Adding <cache> child elements to the et> 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 configuration ehcache default configuration file ehcache.xml (fixed name) (placed under the classpath)

On the Internet to read, said is hibernate in a cache, snapshot, level Two cache interview when asked more, feel the teacher is not clear, so I summed up a bit.

Hibernate and snapshots

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.