Using Query Cache in Hibernate

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div style&equals;"color&colon; &num;20124d&semi;">In current chapter I am going to explain how to use <b>Hibernate Query Cache<&sol;b> to avoid amount of traffic between your application and database&period; The query cache is responsible for caching the results of queries&period; Let us have a look how Hibernate uses the query cache to retrieve objects&period;<&sol;div>&NewLine;<div style&equals;"color&colon; &num;20124d&semi;">&NewLine;<div id&equals;"ads-id" align&equals;"center"><&sol;div>&NewLine;<&sol;div>&NewLine;<div style&equals;"color&colon; &num;20124d&semi;">Note that the query cache does not cache the state of the actual entities in the result set&semi; it caches only identifier values and results of value type&period; So the query cache should always be used in conjunction with the second-level cache&period;<&sol;div>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border&colon; 1px solid &num;c9e6f2&semi; border-radius&colon; 3px&semi; padding&colon; 16px&semi; line-height&colon; 1&period;45&semi;"><span style&equals;"color&colon; red&semi; font-size&colon; x-large&semi; text-align&colon; center&semi;"><b>Popular Tutorials<&sol;b><&sol;span><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-tutorial&sol;"><em><strong>Spring Tutorial<&sol;strong> <&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-web-mvc-framework-chapter-38&sol;"><strong><em>Spring MVC Web Tutorial <&sol;em><&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;introduction-to-spring-boot-a-spring-boot-complete-guide&sol;"><strong>Spring Boot Tutorial<&sol;strong> <&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-take-baby-step-to-secure&sol;"><em>Spring Security Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint&sol;"><em>Spring AOP Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;using-spring-jdbc-framework-chapter-32&sol;"><em>Spring JDBC Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-hateoas-hypermedia-driven-restful-web-service&sol;"><em><strong>Spring HATEOAS <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;microservices-with-spring-boot&sol;"><em><strong>Microservices with Spring Boot<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jax-rs-web-service-tutorial&sol;"><strong><em>REST Webservice<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-baby-step-to-be-best-java-ian&sol;"><em><strong>Core Java <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hibernate-3-on-baby-steps&sol;"><em><strong>Hibernate Tutorial<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-batch-process-with-example&sol;"><strong><em>Spring Batch<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<div><&sol;div>&NewLine;<h2 style&equals;"color&colon; blue&semi;"><b>How the Query Cache Works&colon;<&sol;b><&sol;h2>&NewLine;<pre class&equals;"highlight">Query query &equals; session&period;createQuery&lpar;"from Student as student where student&period;id &equals; &quest; and student&period;studentName &equals; &quest;"&rpar;&semi; &NewLine;query&period;setInt&lpar;0&comma; Integer&period;valueOf&lpar;1&rpar;&rpar;&semi; &NewLine;query&period;setString&lpar;1&comma; "Dinesh Rajput"&rpar;&semi; &NewLine;query&period;setCacheable&lpar;true&rpar;&semi; &NewLine;List l &equals; query&period;list&lpar;&rpar;&semi; &NewLine;<&sol;pre>&NewLine;<&sol;div>&NewLine;<p>The query cache works something like this&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&ast;----------------------------------------------------------------------------------------&ast; &NewLine;&vert; Query Cache &vert; &NewLine;&vert;----------------------------------------------------------------------------------------&vert; &NewLine;&vert; &lbrack;"from Student as student where student&period;id &equals; &quest; and student&period;studentName &equals; &quest;"&comma; &lbrack; 1 &comma; "Dinesh Rajput"&rsqb; &rsqb; -&gt&semi; &lbrack; 2 &rsqb; &rsqb; &vert; &NewLine;&ast;----------------------------------------------------------------------------------------&ast; &NewLine;<&sol;pre>&NewLine;<div style&equals;"color&colon; &num;20124d&semi;">The combination of the query and the values provided as parameters to that query is used as a key&comma; and the value is the list of identifiers for that query&period; Note that this becomes more complex from an internal perspective as you begin to consider that a query can have an effect of altering associations to objects returned that query&semi; not to mention the fact that a query may not return whole objects&comma; but may in fact only return scalar values &lpar;when you have supplied a select clause for instance&rpar;&period; That being said&comma; this is a sound and reliable way to think of the query cache conceptually&period;<&sol;div>&NewLine;<h3>Using the query cache<&sol;h3>&NewLine;<div style&equals;"color&colon; &num;20124d&semi;">Two hibernate properties control whether the query cache is enabled&colon;<&sol;div>&NewLine;<ul style&equals;"color&colon; &num;20124d&semi;">&NewLine;<li>hibernate&period;cache&period;use&lowbar;second&lowbar;level&lowbar;cache&equals;true&vert;false<&sol;li>&NewLine;<li>hibernate&period;cache&period;use&lowbar;query&lowbar;cache&equals;true&vert;false<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<div style&equals;"color&colon; &num;20124d&semi;">The first turns on the second level cache in general and the second turns on the query cache regions&period; If the second is set to false&comma; the query and timestamp cache regions are not created or used&period;<&sol;div>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;04&sol;querycacheworkflow&period;png" width&equals;"613" height&equals;"640" border&equals;"0" &sol;><&sol;div>&NewLine;<p><b>In the <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hibernate-batch-processing&lowbar;10&sol;">Next Chapter<&sol;a> we will discuss more about the <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hibernate-batch-processing&lowbar;10&sol;">Hibernate Batch Processing&period;<&sol;a><&sol;b><&sol;p>&NewLine;<p><b>&lt&semi;&lt&semi;<a style&equals;"font-family&colon; Times&comma; 'Times New Roman'&comma; serif&semi;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;cacheing-in-hibernate-first-level-and&period;html">Previous Chapter 32<&sol;a>&lt&semi;&lt&semi;    &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hibernate-batch-processing&lowbar;10&sol;">Next Chapter 34<&sol;a>&gt&semi;&gt&semi;<&sol;b><br &sol;>&NewLine;<b><br &sol;>&NewLine;<&sol;b> <b><br &sol;>&NewLine;<&sol;b> <b><br &sol;>&NewLine;<&sol;b><&sol;p>&NewLine;<&sol;div>&NewLine;