查询缓存是针对普通属性结果集的缓存
对实体对象的结果集只缓存id
查询缓存的生命周期,当前关联的表发生修改,那么查询缓存生命周期结束
查询缓存的配置和使用:
* 在hibernate.cfg.xml文件中启用查询缓存,如:
<property name="hibernate.cache.use_query_cache">true</property>
* 在程序中必须手动启用查询缓存,如:
query.setCacheable(true);
hibernate.cfg.xml
1<!DOCTYPE hibernate-configuration PUBLIC
2 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
3 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
4<hibernate-configuration>
5 <session-factory>
6 <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_cache</property>
7 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
8 <property name="hibernate.connection.username">root</property>
9 <property name="hibernate.connection.password">bjsxt</property>
10 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
11 <property name="hibernate.show_sql">true</property>
12 <!-- 开启二级缓存 -->
13 <property name="hibernate.cache.use_second_level_cache">true</property>
14 <!-- 指定缓存产品提供商 -->
15 <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
16 <!-- 启用查询缓存 -->
17 <property name="hibernate.cache.use_query_cache">true</property>
18 <mapping resource="com/bjsxt/hibernate/Classes.hbm.xml" />
19 <mapping resource="com/bjsxt/hibernate/Student.hbm.xml" />
20 <class-cache class="com.bjsxt.hibernate.Student" usage="read-only" include="all" />
21 </session-factory>
22 </hibernate-configuration>
1<!DOCTYPE hibernate-configuration PUBLIC
2 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
3 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
4<hibernate-configuration>
5 <session-factory>
6 <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_cache</property>
7 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
8 <property name="hibernate.connection.username">root</property>
9 <property name="hibernate.connection.password">bjsxt</property>
10 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
11 <property name="hibernate.show_sql">true</property>
12 <!-- 开启二级缓存 -->
13 <property name="hibernate.cache.use_second_level_cache">true</property>
14 <!-- 指定缓存产品提供商 -->
15 <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
16 <!-- 启用查询缓存 -->
17 <property name="hibernate.cache.use_query_cache">true</property>
18 <mapping resource="com/bjsxt/hibernate/Classes.hbm.xml" />
19 <mapping resource="com/bjsxt/hibernate/Student.hbm.xml" />
20 <class-cache class="com.bjsxt.hibernate.Student" usage="read-only" include="all" />
21 </session-factory>
22 </hibernate-configuration>
CacheLevel2Test.java
1package com.bjsxt.hibernate;
2
3import java.io.Serializable;
4import java.util.Iterator;
5import java.util.List;
6
7import org.hibernate.CacheMode;
8import org.hibernate.Query;
9import org.hibernate.Session;
10import org.hibernate.SessionFactory;
11
12import junit.framework.TestCase;
13
14public class CacheLevel2Test extends TestCase {
15
16 /** *//**
17 * 开启查询缓存,关闭二级缓存
18 *
19 * 开启一个session,分别调用query.list
20 */
21 public void testCache1() {
22 Session session = null;
23 try {
24 session = HibernateUtils.getSession();
25 session.beginTransaction();
26
27 Query query = session.createQuery("select s.name from Student s");
28 //启用查询查询缓存
29 query.setCacheable(true);
30
31 List names = query.list();
32 for (Iterator iter=names.iterator();iter.hasNext(); ) {
33 String name = (String)iter.next();
34 System.out.println(name);
35 }
36
37 System.out.println("-------------------------------------");
38 query = session.createQuery("select s.name from Student s");
39 //启用查询查询缓存
40 query.setCacheable(true);
41
42 //没有发出查询sql,因为启用了查询缓存
43 names = query.list();
44 for (Iterator iter=names.iterator();iter.hasNext(); ) {
45 String name = (String)iter.next();
46 System.out.println(name);
47 }
48
49 session.getTransaction().commit();
50 }catch(Exception e) {
51 e.printStackTrace();
52 session.getTransaction().rollback();
53 }finally {
54 HibernateUtils.closeSession(session);
55 }
56 }
57
58 /** *//**
59 * 开启查询缓存,关闭二级缓存
60 *
61 * 开启两个session,分别调用query.list
62 */
63 public void testCache2() {
64 Session session = null;
65 try {
66 session = HibernateUtils.getSession();
67 session.beginTransaction();
68
69 Query query = session.createQuery("select s.name from Student s");
70 //启用查询查询缓存
71 query.setCacheable(true);
72
73 List names = query.list();
74 for (Iterator iter=names.iterator();iter.hasNext(); ) {
75 String name = (String)iter.next();
76 System.out.println(name);
77 }
78 session.getTransaction().commit();
79 }catch(Exception e) {
80 e.printStackTrace();
81 session.getTransaction().rollback();
82 }finally {
83 HibernateUtils.closeSession(session);
84 }
85
86 System.out.println("-------------------------------------");
87
88 try {
89 session = HibernateUtils.getSession();
90 session.beginTransaction();
91
92 Query query = session.createQuery("select s.name from Student s");
93 //启用查询查询缓存
94 query.setCacheable(true);
95
96 //不会发出查询sql,因为查询缓存的生命周期和session无关
97 List names = query.list();
98 for (Iterator iter=names.iterator();iter.hasNext(); ) {
99 String name = (String)iter.next();
100 System.out.println(name);
101 }
102 session.getTransaction().commit();
103 }catch(Exception e) {
104 e.printStackTrace();
105 session.getTransaction().rollback();
106 }finally {
107 HibernateUtils.closeSession(session);
108 }
109 }
110
111 /** *//**
112 * 开启查询缓存,关闭二级缓存
113 *
114 * 开启两个session,分别调用query.iterate
115 */
116 public void testCache3() {
117 Session session = null;
118 try {
119 session = HibernateUtils.getSession();
120 session.beginTransaction();
121
122 Query query = session.createQuery("select s.name from Student s");
123 //启用查询查询缓存
124 query.setCacheable(true);
125
126 for (Iterator iter=query.iterate();iter.hasNext(); ) {
127 String name = (String)iter.next();
128 System.out.println(name);
129 }
130 session.getTransaction().commit();
131 }catch(Exception e) {
132 e.printStackTrace();
133 session.getTransaction().rollback();
134 }finally {
135 HibernateUtils.closeSession(session);
136 }
137
138 System.out.println("-------------------------------------");
139
140 try {
141 session = HibernateUtils.getSession();
142 session.beginTransaction();
143
144 Query query = session.createQuery("select s.name from Student s");
145 //启用查询查询缓存
146 query.setCacheable(true);
147
148 //查询缓存只对query.list()起作用,query.iterate不起作用,也就是query.iterate不使用
149 //查询缓存
150 for (Iterator iter=query.iterate();iter.hasNext(); ) {
151 String name = (String)iter.next();
152 System.out.println(name);
153 }
154 session.getTransaction().commit();
155 }catch(Exception e) {
156 e.printStackTrace();
157 session.getTransaction().rollback();
158 }finally {
159 HibernateUtils.closeSession(session);
160 }
161 }
162
163 /** *//**
164 * 关闭查询缓存,关闭二级缓存
165 *
166 * 开启两个session,分别调用query.list查询实体对象
167 */
168 public void testCache4() {
169 Session session = null;
170 try {
171 session = HibernateUtils.getSession();
172 session.beginTransaction();
173
174 Query query = session.createQuery("select s from Student s");
175 //启用查询查询缓存
176 //query.setCacheable(true);
177
178 List students = query.list();
179 for (Iterator iter=students.iterator();iter.hasNext(); ) {
180 Student student = (Student)iter.next();
181 System.out.println(student.getName());
182 }
183 session.getTransaction().commit();
184 }catch(Exception e) {
185 e.printStackTrace();
186 session.getTransaction().rollback();
187 }finally {
188 HibernateUtils.closeSession(session);
189 }
190
191 System.out.println("-------------------------------------");
192
193 try {
194 session = HibernateUtils.getSession();
195 session.beginTransaction();
196
197 Query query = session.createQuery("select s from Student s");
198 //启用查询查询缓存
199 //query.setCacheable(true);
200 //会发出查询sql,因为list默认每次都会发出查询sql
201 List students = query.list();
202 for (Iterator iter=students.iterator();iter.hasNext(); ) {
203 Student student = (Student)iter.next();
204 System.out.println(student.getName());
205 }
206 session.getTransaction().commit();
207 }catch(Exception e) {
208 e.printStackTrace();
209 session.getTransaction().rollback();
210 }finally {
211 HibernateUtils.closeSession(session);
212 }
213 }
214
215 /** *//**
216 * 开启查询缓存,关闭二级缓存
217 *
218 * 开启两个session,分别调用query.list查询实体对象
219 */
220 public void testCache5() {
221 Session session = null;
222 try {
223 session = HibernateUtils.getSession();
224 session.beginTransaction();
225
226 Query query = session.createQuery("select s from Student s");
227 //启用查询查询缓存
228 query.setCacheable(true);
229
230 List students = query.list();
231 for (Iterator iter=students.iterator();iter.hasNext(); ) {
232 Student student = (Student)iter.next();
233 System.out.println(student.getName());
234 }
235 session.getTransaction().commit();
236 }catch(Exception e) {
237 e.printStackTrace();
238 session.getTransaction().rollback();
239 }finally {
240 HibernateUtils.closeSession(session);
241 }
242
243 System.out.println("-------------------------------------");
244
245 try {
246 session = HibernateUtils.getSession();
247 session.beginTransaction();
248
249 Query query = session.createQuery("select s from Student s");
250 //启用查询查询缓存
251 query.setCacheable(true);
252
253 //会发出n条查询语句,因为开启了查询缓存,关闭了二级缓存,那么查询缓存会缓存实体对象的id
254 //所以hibernate会根据实体对象的id去查询相应的实体,如果缓存中不存在相应的
255 //实体那么将发出根据实体id查询的sql语句,否则不会发出sql使用缓存中的数据
256 List students = query.list();
257 for (Iterator iter=students.iterator();iter.hasNext(); ) {
258 Student student = (Student)iter.next();
259 System.out.println(student.getName());
260 }
261 session.getTransaction().commit();
262 }catch(Exception e) {
263 e.printStackTrace();
264 session.getTransaction().rollback();
265 }finally {
266 HibernateUtils.closeSession(session);
267 }
268 }
269
270 /** *//**
271 * 开启查询缓存,开启二级缓存
272 *
273 * 开启两个session,分别调用query.list查询实体对象
274 */
275 public void testCache6() {
276 Session session = null;
277 try {
278 session = HibernateUtils.getSession();
279 session.beginTransaction();
280
281 Query query = session.createQuery("select s from Student s");
282 //启用查询查询缓存
283 query.setCacheable(true);
284
285 List students = query.list();
286 for (Iterator iter=students.iterator();iter.hasNext(); ) {
287 Student student = (Student)iter.next();
288 System.out.println(student.getName());
289 }
290 session.getTransaction().commit();
291 }catch(Exception e) {
292 e.printStackTrace();
293 session.getTransaction().rollback();
294 }finally {
295 HibernateUtils.closeSession(session);
296 }
297
298 System.out.println("-------------------------------------");
299
300 try {
301 session = HibernateUtils.getSession();
302 session.beginTransaction();
303
304 Query query = session.createQuery("select s from Student s");
305 //启用查询查询缓存
306 query.setCacheable(true);
307
308 //不会发出查询sql,因为开启了二级缓存和查询缓存,查询缓存缓存了实体对象的id列表
309 //hibernate会根据实体对象的id列表到二级缓存中取得相应的数据
310 List students = query.list();
311 for (Iterator iter=students.iterator();iter.hasNext(); ) {
312 Student student = (Student)iter.next();
313 System.out.println(student.getName());
314 }
315 session.getTransaction().commit();
316 }catch(Exception e) {
317 e.printStackTrace();
318 session.getTransaction().rollback();
319 }finally {
320 HibernateUtils.closeSession(session);
321 }
322 }
323}
324
1package com.bjsxt.hibernate;
2
3import java.io.Serializable;
4import java.util.Iterator;
5import java.util.List;
6
7import org.hibernate.CacheMode;
8import org.hibernate.Query;
9import org.hibernate.Session;
10import org.hibernate.SessionFactory;
11
12import junit.framework.TestCase;
13
14public class CacheLevel2Test extends TestCase {
15
16 /** *//**
17 * 开启查询缓存,关闭二级缓存
18 *
19 * 开启一个session,分别调用query.list
20 */
21 public void testCache1() {
22 Session session = null;
23 try {
24 session = HibernateUtils.getSession();
25 session.beginTransaction();
26
27 Query query = session.createQuery("select s.name from Student s");
28 //启用查询查询缓存
29 query.setCacheable(true);
30
31 List names = query.list();
32 for (Iterator iter=names.iterator();iter.hasNext(); ) {
33 String name = (String)iter.next();
34 System.out.println(name);
35 }
36
37 System.out.println("-------------------------------------");
38 query = session.createQuery("select s.name from Student s");
39 //启用查询查询缓存
40 query.setCacheable(true);
41
42 //没有发出查询sql,因为启用了查询缓存
43 names = query.list();
44 for (Iterator iter=names.iterator();iter.hasNext(); ) {
45 String name = (String)iter.next();
46 System.out.println(name);
47 }
48
49 session.getTransaction().commit();
50 }catch(Exception e) {
51 e.printStackTrace();
52 session.getTransaction().rollback();
53 }finally {
54 HibernateUtils.closeSession(session);
55 }
56 }
57
58 /** *//**
59 * 开启查询缓存,关闭二级缓存
60 *
61 * 开启两个session,分别调用query.list
62 */
63 public void testCache2() {
64 Session session = null;
65 try {
66 session = HibernateUtils.getSession();
67 session.beginTransaction();
68
69 Query query = session.createQuery("select s.name from Student s");
70 //启用查询查询缓存
71 query.setCacheable(true);
72
73 List names = query.list();
74 for (Iterator iter=names.iterator();iter.hasNext(); ) {
75 String name = (String)iter.next();
76 System.out.println(name);
77 }
78 session.getTransaction().commit();
79 }catch(Exception e) {
80 e.printStackTrace();
81 session.getTransaction().rollback();
82 }finally {
83 HibernateUtils.closeSession(session);
84 }
85
86 System.out.println("-------------------------------------");
87
88 try {
89 session = HibernateUtils.getSession();
90 session.beginTransaction();
91
92 Query query = session.createQuery("select s.name from Student s");
93 //启用查询查询缓存
94 query.setCacheable(true);
95
96 //不会发出查询sql,因为查询缓存的生命周期和session无关
97 List names = query.list();
98 for (Iterator iter=names.iterator();iter.hasNext(); ) {
99 String name = (String)iter.next();
100 System.out.println(name);
101 }
102 session.getTransaction().commit();
103 }catch(Exception e) {
104 e.printStackTrace();
105 session.getTransaction().rollback();
106 }finally {
107 HibernateUtils.closeSession(session);
108 }
109 }
110
111 /** *//**
112 * 开启查询缓存,关闭二级缓存
113 *
114 * 开启两个session,分别调用query.iterate
115 */
116 public void testCache3() {
117 Session session = null;
118 try {
119 session = HibernateUtils.getSession();
120 session.beginTransaction();
121
122 Query query = session.createQuery("select s.name from Student s");
123 //启用查询查询缓存
124 query.setCacheable(true);
125
126 for (Iterator iter=query.iterate();iter.hasNext(); ) {
127 String name = (String)iter.next();
128 System.out.println(name);
129 }
130 session.getTransaction().commit();
131 }catch(Exception e) {
132 e.printStackTrace();
133 session.getTransaction().rollback();
134 }finally {
135 HibernateUtils.closeSession(session);
136 }
137
138 System.out.println("-------------------------------------");
139
140 try {
141 session = HibernateUtils.getSession();
142 session.beginTransaction();
143
144 Query query = session.createQuery("select s.name from Student s");
145 //启用查询查询缓存
146 query.setCacheable(true);
147
148 //查询缓存只对query.list()起作用,query.iterate不起作用,也就是query.iterate不使用
149 //查询缓存
150 for (Iterator iter=query.iterate();iter.hasNext(); ) {
151 String name = (String)iter.next();
152 System.out.println(name);
153 }
154 session.getTransaction().commit();
155 }catch(Exception e) {
156 e.printStackTrace();
157 session.getTransaction().rollback();
158 }finally {
159 HibernateUtils.closeSession(session);
160 }
161 }
162
163 /** *//**
164 * 关闭查询缓存,关闭二级缓存
165 *
166 * 开启两个session,分别调用query.list查询实体对象
167 */
168 public void testCache4() {
169 Session session = null;
170 try {
171 session = HibernateUtils.getSession();
172 session.beginTransaction();
173
174 Query query = session.createQuery("select s from Student s");
175 //启用查询查询缓存
176 //query.setCacheable(true);
177
178 List students = query.list();
179 for (Iterator iter=students.iterator();iter.hasNext(); ) {
180 Student student = (Student)iter.next();
181 System.out.println(student.getName());
182 }
183 session.getTransaction().commit();
184 }catch(Exception e) {
185 e.printStackTrace();
186 session.getTransaction().rollback();
187 }finally {
188 HibernateUtils.closeSession(session);
189 }
190
191 System.out.println("-------------------------------------");
192
193 try {
194 session = HibernateUtils.getSession();
195 session.beginTransaction();
196
197 Query query = session.createQuery("select s from Student s");
198 //启用查询查询缓存
199 //query.setCacheable(true);
200 //会发出查询sql,因为list默认每次都会发出查询sql
201 List students = query.list();
202 for (Iterator iter=students.iterator();iter.hasNext(); ) {
203 Student student = (Student)iter.next();
204 System.out.println(student.getName());
205 }
206 session.getTransaction().commit();
207 }catch(Exception e) {
208 e.printStackTrace();
209 session.getTransaction().rollback();
210 }finally {
211 HibernateUtils.closeSession(session);
212 }
213 }
214
215 /** *//**
216 * 开启查询缓存,关闭二级缓存
217 *
218 * 开启两个session,分别调用query.list查询实体对象
219 */
220 public void testCache5() {
221 Session session = null;
222 try {
223 session = HibernateUtils.getSession();
224 session.beginTransaction();
225
226 Query query = session.createQuery("select s from Student s");
227 //启用查询查询缓存
228 query.setCacheable(true);
229
230 List students = query.list();
231 for (Iterator iter=students.iterator();iter.hasNext(); ) {
232 Student student = (Student)iter.next();
233 System.out.println(student.getName());
234 }
235 session.getTransaction().commit();
236 }catch(Exception e) {
237 e.printStackTrace();
238 session.getTransaction().rollback();
239 }finally {
240 HibernateUtils.closeSession(session);
241 }
242
243 System.out.println("-------------------------------------");
244
245 try {
246 session = HibernateUtils.getSession();
247 session.beginTransaction();
248
249 Query query = session.createQuery("select s from Student s");
250 //启用查询查询缓存
251 query.setCacheable(true);
252
253 //会发出n条查询语句,因为开启了查询缓存,关闭了二级缓存,那么查询缓存会缓存实体对象的id
254 //所以hibernate会根据实体对象的id去查询相应的实体,如果缓存中不存在相应的
255 //实体那么将发出根据实体id查询的sql语句,否则不会发出sql使用缓存中的数据
256 List students = query.list();
257 for (Iterator iter=students.iterator();iter.hasNext(); ) {
258 Student student = (Student)iter.next();
259 System.out.println(student.getName());
260 }
261 session.getTransaction().commit();
262 }catch(Exception e) {
263 e.printStackTrace();
264 session.getTransaction().rollback();
265 }finally {
266 HibernateUtils.closeSession(session);
267 }
268 }
269
270 /** *//**
271 * 开启查询缓存,开启二级缓存
272 *
273 * 开启两个session,分别调用query.list查询实体对象
274 */
275 public void testCache6() {
276 Session session = null;
277 try {
278 session = HibernateUtils.getSession();
279 session.beginTransaction();
280
281 Query query = session.createQuery("select s from Student s");
282 //启用查询查询缓存
283 query.setCacheable(true);
284
285 List students = query.list();
286 for (Iterator iter=students.iterator();iter.hasNext(); ) {
287 Student student = (Student)iter.next();
288 System.out.println(student.getName());
289 }
290 session.getTransaction().commit();
291 }catch(Exception e) {
292 e.printStackTrace();
293 session.getTransaction().rollback();
294 }finally {
295 HibernateUtils.closeSession(session);
296 }
297
298 System.out.println("-------------------------------------");
299
300 try {
301 session = HibernateUtils.getSession();
302 session.beginTransaction();
303
304 Query query = session.createQuery("select s from Student s");
305 //启用查询查询缓存
306 query.setCacheable(true);
307
308 //不会发出查询sql,因为开启了二级缓存和查询缓存,查询缓存缓存了实体对象的id列表
309 //hibernate会根据实体对象的id列表到二级缓存中取得相应的数据
310 List students = query.list();
311 for (Iterator iter=students.iterator();iter.hasNext(); ) {
312 Student student = (Student)iter.next();
313 System.out.println(student.getName());
314 }
315 session.getTransaction().commit();
316 }catch(Exception e) {
317 e.printStackTrace();
318 session.getTransaction().rollback();
319 }finally {
320 HibernateUtils.closeSession(session);
321 }
322 }
323}
324