写测试类
package com.going.oa.CacheTest;
import junit.framework.TestCase;
import com.going.framework.util.HibernateUtil;
import com.going.oa.model.Attachment;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
* Created by IntelliJ IDEA.
* User: weiyong
* Date: 2010-3-25
* Time: 13:12:30
* To change this template use File | Settings | File Templates.
*/
public class CacheTest extends TestCase {
public void testCache() {
//保存数据 用于后期数据的测试
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
//定义附件对象
Attachment at = new Attachment();
at.setAttachmentName("##############");
at.setAttachmentSize("22@");
at.setCreateId("123@");
at.setAttachmentPath("ttttt");
session.save(at);
tx.commit();
HibernateUtil.closeSession();
}
/**
* 测试二级缓存对load方法的影响
*/
public void testLoad() {
Session session = HibernateUtil.currentSession();
//根据记录的Id 通过load方法来获取实体Id为8abf8d0c2793c7f0012793c7f3240001的对象
Attachment attachment = (Attachment) session.load(Attachment.class, "8abf8d0c2793c7f0012793c7f3240001");
System.out.println("***1"+attachment.getAttachmentName());
HibernateUtil.closeSession();
Session session3 = HibernateUtil.currentSession();
//根据记录的Id 通过load方法再一次来获取实体Id为8abf8d0c2793c7f0012793c7f3240001的对象
Attachment attachment3 = (Attachment) session3.load(Attachment.class, "8abf8d0c2793c7f0012793c7f3240001");
System.out.println("***3"+attachment.getAttachmentName());
HibernateUtil.closeSession();
}
/**
* 测试二级缓存对get方法的影响
*/
public void testGet() {
Session session = HibernateUtil.currentSession();
//根据记录的Id 通过get方法来获取实体Id为8abf8d0c2793c7f0012793c7f3240001的对象
Attachment attachment = (Attachment) session.get(Attachment.class, "8abf8d0c2793c7f0012793c7f3240001");
System.out.println("***1"+attachment.getAttachmentName());
HibernateUtil.closeSession();
Session session4 = HibernateUtil.currentSession();
//根据记录的Id 通过get方法再一次来获取实体Id为8abf8d0c2793c7f0012793c7f3240001的对象
Attachment attachment4 = (Attachment) session4.get(Attachment.class, "8abf8d0c2793c7f0012793c7f3240001");
System.out.println("***3"+attachment4.getAttachmentName());
HibernateUtil.closeSession();
}
/**
* 测试二级缓存 对HSql语句的影响
*/
public void testHsql() {
//定义sql语句 并通过hsql语句 来查询数据
String sql = " from Attachment ";
Session session = HibernateUtil.currentSession();
session.createQuery(sql).list();
HibernateUtil.closeSession();
//定义sql语句 并通过相同的hsql语句 来查询数据
String sql2 = " from Attachment ";
Session session2 = HibernateUtil.currentSession();
session2.createQuery(sql2).list();
HibernateUtil.closeSession();
}
/**
* 测试查询缓存 对HSql语句的影响
*/
public void testHsql2() {
//定义sql语句 并通过hsql语句 来查询数据
String sql = " from Attachment ";
Session session = HibernateUtil.currentSession();
List<Attachment> list=session.createQuery(sql).setCacheable(true).list();
System.out.println("***1"+list.size());
HibernateUtil.closeSession();
//定义sql语句 并通过相同的hsql语句 来查询数据
String sql2 = " from Attachment ";
Session session2 = HibernateUtil.currentSession();
List<Attachment> list2=session2.createQuery(sql2).setCacheable(true).list();
System.out.println("***2"+list2.size());
HibernateUtil.closeSession();
}
}
testLoad 的测试结果 ::(发送一次查询)
Hibernate:
/* load com.going.oa.model.Attachment */ select
attachment0_.ATTACHEMENT_ID as ATTACHEM1_0_0_,
attachment0_.ATTACHMENT_NAME as ATTACHMENT2_0_0_,
attachment0_.ATTACHMENT_PATH as ATTACHMENT3_0_0_,
attachment0_.ATTACHMENT_SIZE as ATTACHMENT4_0_0_,
attachment0_.UPLOAD_TIME as UPLOAD5_0_0_,
attachment0_.CREATE_Id as CREATE6_0_0_
from
OA_ATTACHMENT attachment0_
where
attachment0_.ATTACHEMENT_ID=?
***1##############
***3##############
testGet的测试结果 ::(发送一次查询)
Hibernate:
/* load com.going.oa.model.Attachment */ select
attachment0_.ATTACHEMENT_ID as ATTACHEM1_0_0_,
attachment0_.ATTACHMENT_NAME as ATTACHMENT2_0_0_,
attachment0_.ATTACHMENT_PATH as ATTACHMENT3_0_0_,
attachment0_.ATTACHMENT_SIZE as ATTACHMENT4_0_0_,
attachment0_.UPLOAD_TIME as UPLOAD5_0_0_,
attachment0_.CREATE_Id as CREATE6_0_0_
from
OA_ATTACHMENT attachment0_
where
attachment0_.ATTACHEMENT_ID=?
***1##############
***3##############
配置二级缓存 不配置查询缓存时 testHsql的测试结果 (发送俩次查询)
Hibernate:
/*
from
Attachment */ select
attachment0_.ATTACHEMENT_ID as ATTACHEM1_0_,
attachment0_.ATTACHMENT_NAME as ATTACHMENT2_0_,
attachment0_.ATTACHMENT_PATH as ATTACHMENT3_0_,
attachment0_.ATTACHMENT_SIZE as ATTACHMENT4_0_,
attachment0_.UPLOAD_TIME as UPLOAD5_0_,
attachment0_.CREATE_Id as CREATE6_0_
from
OA_ATTACHMENT attachment0_
***12
Hibernate:
/*
from
Attachment */ select
attachment0_.ATTACHEMENT_ID as ATTACHEM1_0_,
attachment0_.ATTACHMENT_NAME as ATTACHMENT2_0_,
attachment0_.ATTACHMENT_PATH as ATTACHMENT3_0_,
attachment0_.ATTACHMENT_SIZE as ATTACHMENT4_0_,
attachment0_.UPLOAD_TIME as UPLOAD5_0_,
attachment0_.CREATE_Id as CREATE6_0_
from
OA_ATTACHMENT attachment0_
***22
配置二级缓存 再配置上查询缓存时 testHsql2的测试结果 (发送一次查询)
Hibernate:
/*
from
Attachment */ select
attachment0_.ATTACHEMENT_ID as ATTACHEM1_0_,
attachment0_.ATTACHMENT_NAME as ATTACHMENT2_0_,
attachment0_.ATTACHMENT_PATH as ATTACHMENT3_0_,
attachment0_.ATTACHMENT_SIZE as ATTACHMENT4_0_,
attachment0_.UPLOAD_TIME as UPLOAD5_0_,
attachment0_.CREATE_Id as CREATE6_0_
from
OA_ATTACHMENT attachment0_
***12
***22
通过测试可以得出
load一条记录后就会缓存到二级缓存中,如果再使用id来load记录时,首先会用id到缓存中去找,如果找到则直接从缓存返回。如果没找到则重新到数据库中load出
来。
get一条记录后就会缓存到二级缓存中,如果再使用id来get记录时,首先会用id到缓存中去找,如果找到则从缓存中返回。如果没找到才从数据库中load出来
但使用hsql获取的list对象,当下次再查询时则仍然需要从数据库中获取