测试代码
package com.lbx.model.test;
import java.util.List;
import junit.framework.TestCase;
import org.hibernate.Session;
import org.hibernate.lucene.Text;
import com.lbx.hibernate.Util.HibUtil;
import com.lbx.model.Category;
import com.lbx.model.Topic;
public class Test extends TestCase {
// 测试缓存问题(session级缓存)
@Text
public void testCache01(){
Session session = HibUtil.getSession();
session.beginTransaction();
//同一个session
Topic t1 = (Topic)session.load(Topic.class, 1);
System.out.println(t1.getTitle());
Topic t2 = (Topic)session.load(Topic.class, 1);
System.out.println(t2.getTitle());
session.beginTransaction().commit();
session.close();
}
@Text
public void testCache02(){
Session session = HibUtil.getSession();
session.beginTransaction();
Topic t1 = (Topic)session.load(Topic.class, 1);
System.out.println(t1.getTitle());
session.beginTransaction().commit();
session.close();
//一个session是不能访问其他session的
Session session2 = HibUtil.getSession();
session2.beginTransaction();
Topic t2 = (Topic)session2.load(Topic.class, 1);
System.out.println(t2.getTitle());
session2.beginTransaction().commit();
session2.close();
}
//测试二级缓存
@Text
public void testCache03(){
Session session = HibUtil.getSession();
session.beginTransaction();
Category t1 = (Category)session.load(Category.class, 9);
System.out.println(t1.getName());
session.beginTransaction().commit();
session.close();
//一个session是不能访问其他session的
Session session2 = HibUtil.getSession();
session2.beginTransaction();
Category t2 = (Category)session2.load(Category.class, 9);
System.out.println(t2.getName());
session2.beginTransaction().commit();
session2.close();
}
//测试查询缓存(都只发一条sql语句)
@Text
public void testCache04(){
Session session = HibUtil.getSession();
session.beginTransaction();
List<Category> lists1 = session.createQuery("from Category").setCacheable(true).list();
List<Category> lists2 = session.createQuery("from Category").setCacheable(true).list();
session.beginTransaction().commit();
session.close();
Session session2 = HibUtil.getSession();
session2.beginTransaction();
List<Category> lists3 = session2.createQuery("from Category").setCacheable(true).list();
List<Category> lists4 = session2.createQuery("from Category").setCacheable(true).list();
session2.beginTransaction().commit();
session2.close();
}
}