论坛首页 Java企业应用论坛

session.get()使用二级缓存

浏览 3791 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-08-30  
         初学hibernate,今天看到session.find()和session.load()方法的区别,书上说load方法可以充分利用二级缓存中的现有数据,而find方法仅在内部缓存进行数据查找,如果没有查找到数据,将越过二级缓存,直接调用SQL完成数据读取。可我试了试,执行结果确实两个方法都充分利用了二级缓存。
代码如下:
  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>
   发表时间:2007-08-31  
LZ说的是load和get吧,session的这两个方法都会从数据库中加载持久化对象,不同的是当DB不错在OID对应的纪录load会抛出异常(ObjectNotFound),而get会返回一个null

0 请登录后投票
   发表时间:2007-08-31  
djhyoo 写道
LZ说的是load和get吧,session的这两个方法都会从数据库中加载持久化对象,不同的是当DB不错在OID对应的纪录load会抛出异常(ObjectNotFound),而get会返回一个null

我想问的是为什么get()方法也使用二级缓存
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics