![](http://static.javashuo.com/static/loading.gif)
package org.qiujy.test.cache;
![](http://static.javashuo.com/static/loading.gif)
import java.util.List;
![](http://static.javashuo.com/static/loading.gif)
import org.hibernate.HibernateException;
![](http://static.javashuo.com/static/loading.gif)
import org.hibernate.Session;
![](http://static.javashuo.com/static/loading.gif)
import org.hibernate.Transaction;
![](http://static.javashuo.com/static/loading.gif)
import org.qiujy.common.HibernateSessionFactory;
![](http://static.javashuo.com/static/loading.gif)
import org.qiujy.domain.cachedemo.Product;
public
class TestCache {
public
static
void main(String[] args) {
//test cache.........
![](http://static.javashuo.com/static/loading.gif)
Session session2 = HibernateSessionFactory.getSession();
![](http://static.javashuo.com/static/loading.gif)
Transaction tx2 =
null;
try{
![](http://static.javashuo.com/static/loading.gif)
tx2 = session2.beginTransaction();
![](http://static.javashuo.com/static/loading.gif)
List list = session2.createQuery(
"from Product").list();
for(
int i = 0 ; i < list.size(); i++){
![](http://static.javashuo.com/static/loading.gif)
Product prod = (Product)list.get(i);
![](http://static.javashuo.com/static/loading.gif)
System.
out.println(prod.getName());
![](http://static.javashuo.com/static/loading.gif)
}
![](http://static.javashuo.com/static/loading.gif)
tx2.commit();
![](http://static.javashuo.com/static/loading.gif)
}
catch(HibernateException e){
if(tx2 !=
null){
![](http://static.javashuo.com/static/loading.gif)
tx2.rollback();
![](http://static.javashuo.com/static/loading.gif)
}
![](http://static.javashuo.com/static/loading.gif)
e.printStackTrace();
![](http://static.javashuo.com/static/loading.gif)
}
finally{
![](http://static.javashuo.com/static/loading.gif)
HibernateSessionFactory.closeSession();
![](http://static.javashuo.com/static/loading.gif)
}
//-------------------
![](http://static.javashuo.com/static/loading.gif)
Session session3 = HibernateSessionFactory.getSession();
![](http://static.javashuo.com/static/loading.gif)
Transaction tx3 =
null;
try{
![](http://static.javashuo.com/static/loading.gif)
tx3 = session3.beginTransaction();
![](http://static.javashuo.com/static/loading.gif)
Product prod = (Product)session3.get(Product.
class,
new Long(1));
![](http://static.javashuo.com/static/loading.gif)
System.
out.println(
"從cache中獲得,不執行SQL---" + prod.getName());
![](http://static.javashuo.com/static/loading.gif)
tx3.commit();
![](http://static.javashuo.com/static/loading.gif)
}
catch(HibernateException e){
if(tx3 !=
null){
![](http://static.javashuo.com/static/loading.gif)
tx3.rollback();
![](http://static.javashuo.com/static/loading.gif)
}
![](http://static.javashuo.com/static/loading.gif)
e.printStackTrace();
![](http://static.javashuo.com/static/loading.gif)
}
finally{
![](http://static.javashuo.com/static/loading.gif)
HibernateSessionFactory.closeSession();
![](http://static.javashuo.com/static/loading.gif)
}
![](http://static.javashuo.com/static/loading.gif)
}
![](http://static.javashuo.com/static/loading.gif)
}