Third, Hibernate advanced

Source: Internet
Author: User

1. Query Inquiry method
SQL,HQL,OID,QBC (query by criteria)
2, Level two cache
Sessionfactory cache, also called process level cache, implemented using 3rd party plug-ins, caches entities only, lifecycle and sessionfactory consistent
Usage Environment:
are often accessed
Little change
Limited quantity
Data that is not very important, allowing occasional concurrent data to occur
Method of Use: (General use Ehcache)
Importing JAR Packages
In the Hibernate.cfg.xml file
Set up to use level two cache <property name= "Cache.use_second_level_cache" >true</property>
Specify the cache policy Ehcache
<property name= "Hibernate.cache.provider_class" >
Org.hibernate.cache.EhCacheProvider
</property>
Configure Ehcache.xml, custom cache policy, total 5 properties required
Select which object uses a level two caching mechanism
Configure how to use the level two cache mechanism in the object's configuration file *.hbm.xml file: <cache usage= "read-only" region= "SampleCache1"/>
First, specify the level two cache to use through the region attribute;
Second, use the Usage property to specify how level two caching is used
3. Query cache
Overview: Caching of normal properties. If the associated table changes, the lifecycle of the query cache is also over
Usage Environment:
Queries that are not easily changed by query results, such as product information, that are applicable to the large amount of query result data
How to use:
Add <property name= "Hibernate.cache.use_query_cache" to the Hibernate.cfg.xml file >ture</property>
The query cache must be enabled manually in the program: Query.setcacheable (TRUE);
4. Lock mechanism
Pessimistic lock + optimistic lock
Pessimistic lock: When a thread's transaction is not over, all other transactions are waiting.
Optimistic Lock: Who is in front, who first operation
Implementation principle: Add a field version to the database table, and when the user reads the data, the version number is read out together,
If the user modifies the data, the read version number is compared to the version number in the database,
If the change is the same, the version number will be version+1 after the modification is completed.
If it is not the same, it cannot be modified and throws an exception
How to use:
Object Increment Version Property
The class node of the map file *.hbm.xml adds the attribute optimistic-lock= "lock"
Add node <version name= "version" Type= "integer" column= "T_version"/> must be placed after ID

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.