当前位置: 代码迷 >> Java Web开发 >> [求助] 关于get()方法是否使用二级缓存的测试
  详细解决方案

[求助] 关于get()方法是否使用二级缓存的测试

热度:148   发布时间:2007-08-31 10:38:29.0
[求助] 关于get()方法是否使用二级缓存的测试

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 get1()
{
SessionFactory sessionFactory=new Configuration().configure("org/xiyou/hibernate.cfg.xml").buildSessionFactory();
Session session=sessionFactory.openSession();
Tuser user=(Tuser)session.get(Tuser.class, new Integer(1));
System.out.println(user.getVersion());

session.close();
Session session1=sessionFactory.openSession();
Tuser user1=(Tuser)session1.get(Tuser.class, new Integer(1));
System.out.println(user1.getVersion());

session.close();
System.out.println("session和session1是否相同--->"+(session.equals(session1)));
sessionFactory.close();
System.out.println("-----------------------------------------------------");
}
public void get2()
{
SessionFactory sessionFactory=new Configuration().configue("org/xiyou/hibernate.cfg.xml").buildSessionFactory();
Session session=sessionFactory.openSession();
Tuser user=(Tuser)session.get(Tuser.class, new Integer(1));
System.out.println(user.getVersion());

session.close();
session.getSessionFactory().evict(Tuser.class);
Session session1=sessionFactory.openSession();
Tuser user1=(Tuser)session1.get(Tuser.class, new Integer(1));
System.out.println(user1.getVersion());

session.close();
System.out.println("session和session1是否相同--->"+(session.equals(session1)));
sessionFactory.close();
System.out.println("-----------------------------------------------------");
}
}

测试类:

package org.xiyou;

public class Test01 {
public static void main(String [] args)
{
TuserDAO userdao=new TuserDAO();
System.out.println("使用session.getSessionFactory().evict(Tuser.class);");
userdao.get2();
System.out.println("未使用session.getSessionFactory().evict(Tuser.class);");
userdao.get1();

}
}

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>

encache.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>
控制台输出:
使用session.getSessionFactory().evict(Tuser.class);
Hibernate: select tuser0_.id as id0_, tuser0_.version as version0_0_, tuser0_.name as name0_0_ from test.tuser tuser0_ where tuser0_.id=?
2
Hibernate: select tuser0_.id as id0_, tuser0_.version as version0_0_, tuser0_.name as name0_0_ from test.tuser tuser0_ where tuser0_.id=?
2
session和session1是否相同--->false
-----------------------------------------------------
未使用session.getSessionFactory().evict(Tuser.class);
Hibernate: select tuser0_.id as id0_, tuser0_.version as version1_0_, tuser0_.name as name1_0_ from test.tuser tuser0_ where tuser0_.id=?
2
2
session和session1是否相同--->false
-----------------------------------------------------
各位大侠,谁能告诉我,为什么??get()方法应该只使用内部缓存啊??
搜索更多相关主题的帖子: get  import  二级缓存  org  hibernate  

----------------解决方案--------------------------------------------------------
郁闷啊,没人回复
----------------解决方案--------------------------------------------------------