Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Hibernate's Query cache

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use Hibernate's Query cache, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

To be honest, it is not easy to decide which data needs to come from JDBC or CACHE before JDBC query. But HIBERNATE did the job well. QueryCache is used to cache query statements and query Identifier and Type of objects in the result set. When the cached Query is used again, the actual object can be found in the SECOND LEVEL CACHE through the object's Identifier and Type.

When using QueryCache in hibernate, you need to set the following properties in the hibernate configuration file:

< property name="cache.provider_class">

Org.hibernate.cache.HashtableCacheProvider

< /property>

< property name="hibernate.cache.use_query_cache">

True

< /property>

Set up the configuration file ehcache.xml for ehcache and put it under classpath

< ehcache>

< diskStore path="java.io.tmpdir"/>

< defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" />

< cache name="com.fhway.hibernate.bean.Employee" maxElementsInMemory="10" eternal="false" timeToIdleSeconds="100" timeToLiveSeconds="100" overflowToDisk="false" />

< cache name="com.fhway.hibernate.bean.Department" maxElementsInMemory="10" eternal="false" timeToIdleSeconds="100" timeToLiveSeconds="100" overflowToDisk="false" />

< /ehcache>

Add to the configuration file

< cache>

Such as

< class name="com.fhway.hibernate.bean.Employee " table=" Employee ">

< cache usage="read-only"/>

The strategies that can be set include read-only, read-write, nonstrict-read-write and transactional. Not every third-party cache implementation supports all options. For the timing and supported products of each option, you can directly refer to the 20.2.The Second Level Cache of the official reference book of Hibernate.

In the program, you need to set the Cachable property for the Query object:

Query query = sess.createQuery ("from Employee as employee"); query.setCacheable (true); List employees = (List) query.list (); Iterator iterator = employees.iterator (); while (iterator.hasNext ()) {System.out.println (Employee) iterator.next ());} Query query1 = sess.createQuery ("from Employee as employee"); query1.setCacheable (true); List employees1 = (List) query1.list (); Iterator iterator1 = employee1.iterator () While (iterator1.hasNext ()) {System.out.println ((Employee) iterator1.next ());} Employee goncha = (Employee) sess.load (Employee.class, "001"); System.out.println (goncha)

When you call the above code, you will find this output:

Hibernate: select employee0_.ID as ID, employee0_.NAME as NAME0_, employee0_.DEPNO0 as DEPNO0_ from AFLYER.EMPLOYEE employee0_ com.fhway.hibernate.bean.Employee@e020c9 com.fhway.hibernate.bean.Employee@117f31e com.fhway.hibernate.bean.Employee@bad8a8 com.fhway.hibernate.bean.Employee@104c575 com.fhway.hibernate.bean.Employee@e020c9 com.fhway.hibernate.bean.Employee@117f31e com.fhway.hibernate.bean.Employee@bad8a8 com.fhway.hibernate.bean.Employee@104c575 com.fhway.hibernate.bean.Employee@e020c9

It is clear that the cache is utilized in a way that works for Query and load ()!

There are list () and iterator () methods on Query. The difference between the two methods is that when reading data, the list () method does not make use of the cache, but directly queries the database, while iterator () writes the read data to the cache and uses it again when reading. (Blob cannot use cache)

The above is all the contents of the article "how to use Hibernate's Query cache". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report