hibernate3学习笔记(二十二)|Query缓存机制 - 编程入门网
hibernate3学习笔记(二十二)|Query缓存机制时间:2011-02-03接前面的例子:http://blog.csdn.net/kunshan_shenbin/archive/2008/09/03/2874375.aspx 测试代码如下: 1.package com.hb3.pack_22;2.3.import java.io.IOException;4.import java.sql.SQLException;5.import java.util.List;6. 7.import org.hibernate.Query;8.import org.hibernate.Session;9.import org.hibernate.SessionFactory;10.import org.hibernate.cfg.Configuration;11. 12.import com.hb3.pack_22.model.User;13. 14.public class BusinessService {15. 16. public static void main(String[] args) throws IOException, SQLException {17.18. Configuration config = new Configuration().configure();19. SessionFactory sessionFactory = config.buildSessionFactory(); 20. Session session = sessionFactory.openSession();21. 22.23. String hql = "from com.hb3.pack_22.model.User";24.25. Query query = session.createQuery(hql);26. List<?> users = query.list();27.28. for(int i = 0; i < users.size(); i++) {29. User user = (User) users.get(i);30. System.out.println(user.getName());31. }32.33. query = session.createQuery(hql);34. users = query.list();35.36. for(int i = 0; i < users.size(); i++) {37. User user = (User) users.get(i);38. System.out.println(user.getName());39. }40. 41. 42. session.close();43. sessionFactory.close();44. }45.} hibernate3学习笔记(二十二)|Query缓存机制(2)时间:2011-02-03执行的结果为:
可见,没有启用Query的快取功能。 如果打算打开Query的快取功能,首先在hibernate.cfg.xml中设定hibernate.cache.use_query_cache属性: 1.<?xml version="1.0" encoding="utf-8"?> 2.<!DOCTYPE hibernate-configuration PUBLIC3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">5.6.<hibernate-configuration> 7. 8. <session-factory> 9. ....10. 11. <property name="hibernate.cache.use_query_cache">true</property> 12. ....13.14. </session-factory> 15. 16.</hibernate-configuration> 然后在每次简历Query时,执行setCacheable(true): 1.package com.hb3.pack_22;2.3.import java.io.IOException;4.import java.sql.SQLException;5.import java.util.List;6. 7.import org.hibernate.Query;8.import org.hibernate.Session;9.import org.hibernate.SessionFactory;10.import org.hibernate.cfg.Configuration;11. 12.import com.hb3.pack_22.model.User;13. 14.public class BusinessService {15. 16. public static void main(String[] args) throws IOException, SQLException {17.18. Configuration config = new Configuration().configure();19. |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |