首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

hibernate 中的get步骤与二级缓存

2012-12-14 
hibernate 中的get方法与二级缓存环境.hibernate3.jar ,sql2000,tomcat 6,oscach二级缓存从书上与网上都写

hibernate 中的get方法与二级缓存

环境.hibernate3.jar ,sql2000,tomcat 6,oscach二级缓存
从书上与网上都写的是get方法不会从二级缓存中查找数据,LZ今天心血来潮写了这么一个测试方法


Session s = empeelyDao.getSession();
Empeely emp = (Empeely)s.get(Empeely.class,1);
System.out.println(emp.getName());
Statistics ss = s.getSessionFactory().getStatistics();
System.out.println(ss);
s.close();


Session sss = empeelyDao.getSession();
Empeely emp1  = (Empeely)sss.get(Empeely.class,1);

sss.close();
Statistics ss1 = s.getSessionFactory().getStatistics();
System.out.println(ss1);
System.out.println(emp1.getName());

然后开启二级缓存,控制台输入出

skillStatistics[start time=1350061953385,sessions opened=3,sessions closed=2,transactions=2,successful transactions=2,optimistic lock failures=0,flushes=2,connections obtained=3,statements prepared=5,statements closed=5,second level cache puts=1,second level cache hits=0,second level cache misses=1,entities loaded=1,entities updated=0,entities inserted=2,entities deleted=0,entities fetched=0,collections loaded=0,collections updated=0,collections removed=0,collections recreated=0,collections fetched=0,queries executed to database=0,query cache puts=0,query cache hits=0,query cache misses=0,max query time=0]
Statistics[start time=1350061953385,sessions opened=4,sessions closed=4,transactions=2,successful transactions=2,optimistic lock failures=0,flushes=2,connections obtained=3,statements prepared=5,statements closed=5,second level cache puts=1,second level cache hits=1,second level cache misses=1,entities loaded=1,entities updated=0,entities inserted=2,entities deleted=0,entities fetched=0,collections loaded=0,collections updated=0,collections removed=0,collections recreated=0,collections fetched=0,queries executed to database=0,query cache puts=0,query cache hits=0,query cache misses=0,max query time=0]
skill
这里skill是getName的值.
从这这里没有SQL语句输出.
LZ关闭二级缓存执行同样的代码

skill
Statistics[start time=1350062954436,sessions opened=3,sessions closed=2,transactions=2,successful transactions=2,optimistic lock failures=0,flushes=2,connections obtained=3,statements prepared=5,statements closed=5,second level cache puts=0,second level cache hits=0,second level cache misses=0,entities loaded=1,entities updated=0,entities inserted=2,entities deleted=0,entities fetched=0,collections loaded=0,collections updated=0,collections removed=0,collections recreated=0,collections fetched=0,queries executed to database=0,query cache puts=0,query cache hits=0,query cache misses=0,max query time=0]
Hibernate: select empeely0_.id as id0_0_, empeely0_.firstCard as firstCard0_0_, empeely0_.endCard as endCard0_0_, empeely0_.name as name0_0_, empeely0_1_.skills as skills1_0_, empeely0_2_.yeji as yeji2_0_, case when empeely0_1_.empeely_id is not null then 1 when empeely0_2_.empeely_id is not null then 2 when empeely0_.id is not null then 0 end as clazz_0_ from empeely empeely0_ left outer join skiller empeely0_1_ on empeely0_.id=empeely0_1_.empeely_id left outer join xiaoshou empeely0_2_ on empeely0_.id=empeely0_2_.empeely_id where empeely0_.id=?


01:29:14,756  WARN JDBCExceptionReporter:77 - SQL Warning: 0, SQLState: 
01:29:14,756  WARN JDBCExceptionReporter:78 - [Microsoft][SQLServer 2000 Driver for JDBC]Database changed to acc
01:29:14,756  WARN JDBCExceptionReporter:77 - SQL Warning: 0, SQLState: 
01:29:14,756  WARN JDBCExceptionReporter:78 - [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]已将数据库上下文改为 'acc'。
01:29:14,757  WARN JDBCExceptionReporter:77 - SQL Warning: 0, SQLState: 
01:29:14,757  WARN JDBCExceptionReporter:78 - [Microsoft][SQLServer 2000 Driver for JDBC]Language changed to 简体中文
01:29:14,757  WARN JDBCExceptionReporter:77 - SQL Warning: 0, SQLState: 
01:29:14,757  WARN JDBCExceptionReporter:78 - [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]已将语言设置改为 简体中文。
Statistics[start time=1350062954436,sessions opened=4,sessions closed=4,transactions=2,successful transactions=2,optimistic lock failures=0,flushes=2,connections obtained=4,statements prepared=6,statements closed=6,second level cache puts=0,second level cache hits=0,second level cache misses=0,entities loaded=2,entities updated=0,entities inserted=2,entities deleted=0,entities fetched=0,collections loaded=0,collections updated=0,collections removed=0,collections recreated=0,collections fetched=0,queries executed to database=0,query cache puts=0,query cache hits=0,query cache misses=0,max query time=0]
skill


此时出现SQL查询,求解....

[解决办法]
顶顶...................
[解决办法]
好吧没人回答我自已回答:
存入2级缓存时save,saveorupdate,update,list,iterator,get,load,query,critera都会存入,但是save存入有限制,主键生成方式不能是native的,是native就不会存入2级.
默认情况下没有开启查询缓存时只有iterator,get,load从二级缓存中取数据。当开启查询缓存,并setCacheable(true)时query,critera会从二级缓存中查据。
所以其实get会从二级缓存中查数据。

热点排行