Setting up the environment of hibernate level two cache Ehcache

Source: Internet
Author: User
Tags set set
Common Cache Components



By default, Hibernate uses Ehcache as a level two cache component. However, you can specify a different caching policy by setting the Hibernate.cache.provider_class property, which must implement the Org.hibernate.cache.CacheProvider interface.
By implementing the Org.hibernate.cache.CacheProvider interface, you can provide support for different level two cache components, which act as adapters between cache Plug-ins and hibernate. configuration of the Ehcache environment

On the basis of Hibernate basic environment to continue to configure, Hibernate basic environment to build please refer to the previous blog post MyEclipse build hibernate environment One, add jar

Ehcache is a third-party plug-in that needs to be added with the appropriate jar pack, which is in the Hibernate development Kit we downloaded, I downloaded is hibernate-release-5.0.1.final, how to download the open contract and source code please refer to how to download hibernate source and jar package tutorials from the official website, corresponding Ehcache required jar in hibernate-release-5.0.1.final Under the \lib\optional\ehcache path
Add them to the project's Lib directory and build Path two to open level two caching in the main configuration file

Hibernate the level two cache is turned off by default, and opening needs to be added to the main configuration file
<property name= "Hibernate.cache.use_second_level_cache" >true</property> third, registered level two cache area factory

configuring in the primary configuration file

<property name= "Hibernate.cache.region.factory_class" >net.sf.ehcache.hibernate.ehcacheregionfactory</ Property>

The value of this configuration is not fixed, depending on your hibernate version.


Its value is the full name of this class (no class)
Four, add ehcache.xml file

This file is in the Ehcache-core-2.4.3.jar just added, get the method is: Unzip this jar file


Copy the Ehcache-failsafe.xml file and modify the file name to Ehcache.xml to the SRC folder

v. Configure the class to be cached

Method One: In the mapping file (Xxx.hbm.xml), under the class label add <cache usage= "Read-only"/>
Method Two: Add <class-cache usage= "read-only" class= "Com.hibernate.beans.Student" in the main configuration file (Hibernate.cfg.xml) under the mapping label /> Six, configure the set set to cache

Method One: In the mapping file (xxx.hbm.xml), the set label in front of the key tag to add <cache usage= "Read-only"/>
Method Two: Add <collection-cache usage= "Read-only" in the main configuration file (hibernate.cfg.xml) under Mapping tags Com.hibernate.beans.Student.courses "/>

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.