说起来自己使用hibernate已经大半年了,但一直在做一些学校的toy项目,基本上没有涉足hibernate的高级特性。今天做了下简单的尝试,把一些经验教训记录在本篇博客中,以此为鉴......配置一:hibernate.cfg.xml文件中增加<!--开启二级缓存--><property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property> <property name="hibernate.cache.use_query_cache">true</property>配置二:工程项目src文件下新建一个ehcache.xml文件,其内容为<!--开启二级缓存--><?xml version="1.0" encoding="UTF-8"?><ehcache><diskStore path="java.io.tmpdir" /><defaultCache maxElementsInMemory="10000" eternal="false" overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="180" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" /></ehcache>配置三:为了缓存某类的对象,其hbm文件中需添加<cache usage="read-only"/>属性例如:<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Mapping file autogenerated by MyEclipse - Hibernate Tools--><hibernate-mapping> <class name="com.vogue.bbsphoto.entity.Forum" table="cdb_forums"> <cache usage="read-only"/> <id name="ID" column="fid" unsaved-value="null"> <generator class="increment" /> </id> <property name="name" column="name" type="string" /> <property name="type" column="type" type="string" /> </class></hibernate-mapping>配置四:为了使用查询缓存,Query必须设置cacheable为true,query.setCacheable(true);例如dao父类中用于hql查询的方法修改后为:/** * 执行hql语句的查询 * @param sql * @return */ public List executeQuery(String hql){ List list = new ArrayList(); Session session = HibernateSessionFactory.currentSession(); Transaction tx = null; Query query = session.createQuery(hql); query.setCacheable(true); try { tx = session.beginTransaction(); list = query.list(); tx.commit(); } catch (Exception ex) { ex.printStackTrace(); HibernateSessionFactory.rollbackTransaction(tx); } finally { HibernateSessionFactory.closeSession(); } return list; }实验结果:一验证对象缓存执行如下测试方法public void testLoadForum(){ //第一次加载 long l1 = System.currentTimeMillis(); Forum res = (Forum)fDAO.get(Forum.class,new Long(3)); System.out.println(System.currentTimeMillis() - l1); System.out.println(res.getName()); //第二次加载 long l2 = System.currentTimeMillis(); res = (Forum)fDAO.get(Forum.class,new Long(3)); System.out.println(System.currentTimeMillis() - l2); System.out.println(res.getName());}输出结果:可以看到先后两次加载id为3的(Forum)对象,但是只执行了一条sqllog4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).log4j:WARN Please initialize the log4j system properly.Hibernate: select forum0_.fid as fid0_, forum0_.name as name2_0_, forum0_.type as type2_0_ from cdb_forums forum0_ where forum0_.fid=?1703功能版区0功能版区二验证查询结果缓存执行如下测试方法public void testGetAllForum() { //第一次查询所有论坛记录 long l1 = System.currentTimeMillis(); List res = fDAO.getAllForum(); System.out.println(System.currentTimeMillis() - l1); System.out.println(res.size()); //第二次查询所有论坛记录 long l2 = System.currentTimeMillis(); res = fDAO.getAllForum(); System.out.println(System.currentTimeMillis() - l2); System.out.println(res.size()); }输出结果:先后两次查询所有论坛记录但,仍然只执行了一条sqllog4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).log4j:WARN Please initialize the log4j system properly.Hibernate: select forum0_.fid as fid, forum0_.name as name2_, forum0_.type as type2_ from cdb_forums forum0_ where (1=1 )and(forum0_.type='forum' ) order by forum0_.fid176514014补充一下:当要缓存的对象处于级联关系中时。如果和他存在级联关系的对象都有属性
<cache usage="read-only"/>那么,在第一次get后该对象所处的对象图中的所有对象都会保存到hibernate的二级缓存中,在第二次get该对象时,直接从二级缓存中找到所有级联的对象;如果其中某个级联对象没有
<cache usage="read-only"/>属性,则不会被保存到二级缓存中,
以后每次get时仍然会执行sql去数据库中找该级联对象。
转载请注明原文地址: https://ibbs.8miu.com/read-20656.html