| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 653 人关注过本帖
标题:[求助] 关于get()方法是否使用二级缓存的测试
只看楼主 加入收藏
sea7
Rank: 2
等 级:新手上路
威 望:3
帖 子:291
专家分:0
注 册:2007-2-17
收藏
 问题点数:0 回复次数:1 
[求助] 关于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 
2007-08-31 10:38
sea7
Rank: 2
等 级:新手上路
威 望:3
帖 子:291
专家分:0
注 册:2007-2-17
收藏
得分:0 
郁闷啊,没人回复

软件是一种修养
2007-09-01 22:13
快速回复:[求助] 关于get()方法是否使用二级缓存的测试
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028260 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved