1 @org. Junit.test2 Public voidTesthibernatesecondlevelcache () {3Employee employee= (Employee) Session.get (employee).class, 7499);4 System.out.println (Employee.getname ());5 6 transaction.commit ();7 session.close ();8Session =sessionfactory.opensession ();9Transaction =session.begintransaction ();Ten OneEmployee employee2= (Employee) Session.get (employee).class, 7499); A System.out.println (Employee2.getname ()); -}
Now this code is a two session. Two SQL statements are sent.
One: Hibernate level two cache app
1: Add Jar Package
2: Join Ehcache.xml
<ehcache> <!--sets the path to the directory where cache. data files is created. If the path is a Java System, the It is replaced by its value in the running VM. The following properties are Translated:user.home-User's home directory User.dir-user ' s current working directory Java.io.tmpdir-default temp file Pat H--<diskstore path= "Java.io.tmpdir"/> <!--Default Cache configuration. These would applied to caches programmatically created through the CacheManager. The following attributes is required fordefaultcache:maxinmemory-Sets the maximum number of objects that would be a created in memory eternal-sets whether elements is eternal. If Eternal, timeouts is ignored and the element is never expired. Timetoidleseconds-Sets the time to idle forAn element before it expires. is only usedifThe element is not eternal. Idle time is now-Last accessed time timetoliveseconds-Sets the time to live forAn element before it expires. is only usedifThe element is not eternal. TTL is now-creation Time Overflowtodisk-Sets whether elements can overflow to disk when the in-Memory cache has reached the maxinmemory limit. -<Defaultcache maxelementsinmemory= "10000"Eternal= "false"Timetoidleseconds= "120"Timetoliveseconds= "120"Overflowtodisk= "true"/> <!--predefined caches. ADD your cache configuration settings here. If You DoNot has a configuration foryour cache a WARNING would be issued when the CacheManager starts the following attributes is required forDefaultcache:name-sets the name of the cache. This was used to identify the cache. It must be unique. Maxinmemory-Sets the maximum number of objects that would be a created in memory eternal-sets whether elements is eternal. If Eternal, timeouts is ignored and the element is never expired. Timetoidleseconds-Sets the time to idle forAn element before it expires. is only usedifThe element is not eternal. Idle time is now-Last accessed time timetoliveseconds-Sets the time to live forAn element before it expires. is only usedifThe element is not eternal. TTL is now-creation Time Overflowtodisk-Sets whether elements can overflow to disk when the in-Memory cache has reached the maxinmemory limit. -<!--Sample Cache named SampleCache1 This cache contains a maximum in memory of10000elements, and would expire an elementifIt is idle forMore than 5 minutes and lives forMore than10minutes. If there is more than10000elements It'll overflow to the disk cache, which in Thisconfiguration would go to wherever java.io.tmp are defined on your system. On a standard Linux system ThisWould be/tmp"--<cache name= "SampleCache1"maxelementsinmemory= "10000"Eternal= "false"Timetoidleseconds= "300"Timetoliveseconds= "600"Overflowtodisk= "true"/> <!--Sample Cache named SampleCache2 this cache contains1000elements. Elements is always being held in memory. They is not expired. --<cache name= "SampleCache2"maxelementsinmemory= "1000"Eternal= "true"Timetoidleseconds= "0"Timetoliveseconds= "0"Overflowtodisk= "false"/>-<!--place configuration forYour caches following--></ehcache>
3: Configure the configuration that needs to be cached in the Hibernate.cfg.xml file.
<!--Configure Hibernate-enabled level two cache-- <property name= "Cache.use_second_level_cache" >true</ Property> <!--Configure hibernate two cache for products-- <property name= "Hibernate.cache.region.factory_class" >org.hibernate.cache.ehcache.EhCacheRegionFactory</property> <!--Configure which classes use Hibernate's Level two cache . In mapping back-- <classclass= "Com.hq.entities.Employee"/>
Or in the employee's Hbm.xml file, add
<cache usage= "Read-write"/>
* *: Configuration of the two-level cache for the collection:
<classclass= "Com.hq.entities.Employee"/> <classclass= " Com.hq.entities.Department "/>
or add in Department.hbm.xml and Employee.hbm.xml.
<cache usage= "Read-write"/>
Then add the cache to the collection
<set name= "Emps" table= "Gg_emp" inverse= "true" lazy= "true" > <cache usage= "Read-write"/> <key > <column name= "dept_id"/> </key> class= "Com.hq.entities.Employee"/> </set>
***. Query caching: By default, the set cache is not valid for HQL and QBC queries, but can be made valid in the following ways
I. Declaring open query cache in Hibernate configuration file
<property name= "Cache.use_query_cache" >true</property>
II. Setcacheable (True) method to invoke Query or Criteria
Iii. query caching relies on level two caching
Hibernate level Two Cache