浏览 3788 次
锁定老帖子 主题:session.get()使用二级缓存
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-08-30
代码如下: TuserDAO package org.xiyou; import java.util.Iterator; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.criterion.Expression; public class TuserDAO { public void get() { SessionFactory sessionFactory=new Configuration().configure("org/xiyou/hibernate.cfg.xml").buildSessionFactory(); Session session=sessionFactory.openSession(); Tuser user=(Tuser)session.get(Tuser.class, new Integer(3)); System.out.println(user.getName()); session.close(); Session session1=sessionFactory.openSession(); Tuser user1=(Tuser)session1.get(Tuser.class, new Integer(3)); System.out.println(user1.getName()); System.out.println((session.equals(session1))); session1.close(); } /* * 测试Session.load() * * * */ public void load() { SessionFactory sessionFactory=new Configuration().configure("org/xiyou/hibernate.cfg.xml").buildSessionFactory(); Session session=sessionFactory.openSession(); Tuser user=(Tuser)session.load(Tuser.class, new Integer(3)); System.out.println(user.getName()); session.close(); Session session1=sessionFactory.openSession(); Tuser user1=(Tuser)session1.load(Tuser.class, new Integer(3)); System.out.println(user1.getName()); session1.close(); System.out.println((session.equals(session1))); } } 测试类: package org.xiyou; public class Test01 { public static void main(String [] args) { TuserDAO userdao=new TuserDAO(); userdao.get2(); userdao.load(); } } 执行结果: Hibernate: select tuser0_.id as id0_, tuser0_.version as version0_0_, tuser0_.name as name0_0_ from test.tuser tuser0_ where tuser0_.id=? aaaa aaaa false aaaa aaaa false hibernate.cfg.xml <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="connection.url"> jdbc:mysql://localhost/test </property> <property name="connection.username">root</property> <property name="connection.password">123</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="myeclipse.connection.profile">mysql</property> <property name="show_sql">true</property> <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> <mapping resource="org/xiyou/Tuser.hbm.xml" /> </session-factory> </hibernate-configuration> Tuser.hbm.xml <?xml version="1.0" encoding="utf-8"?> <!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="org.xiyou.Tuser" table="tuser" catalog="test" > <cache usage="read-only"/> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="native" /> </id> <property name="version" column="version" type="integer"></property> <property name="name" type="java.lang.String"> <column name="name" length="20" /> </property> </class> </hibernate-mapping> ehcache.xml <?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd"> <diskStore path="java.io.tmpdir" /> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> </ehcache> 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2007-08-31
LZ说的是load和get吧,session的这两个方法都会从数据库中加载持久化对象,不同的是当DB不错在OID对应的纪录load会抛出异常(ObjectNotFound),而get会返回一个null
|
|
返回顶楼 | |
发表时间:2007-08-31
djhyoo 写道 LZ说的是load和get吧,session的这两个方法都会从数据库中加载持久化对象,不同的是当DB不错在OID对应的纪录load会抛出异常(ObjectNotFound),而get会返回一个null
我想问的是为什么get()方法也使用二级缓存
|
|
返回顶楼 | |