首页>代码>spring整合hibernate+ehcache实现最简单的入门代码demo实例,对初学者很有帮助>/HibernateEHCacheExample/src/main/java/com/zuidaima/hibernate/main/HibernateEHCacheMain.java
package com.zuidaima.hibernate.main;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.stat.Statistics;

import com.zuidaima.hibernate.model.Employee;
import com.zuidaima.hibernate.util.HibernateUtil;

public class HibernateEHCacheMain {

	public static void main(String[] args) {
		
		System.out.println("Temp Dir:"+System.getProperty("java.io.tmpdir"));
		
		//Initialize Sessions
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Statistics stats = sessionFactory.getStatistics();
		System.out.println("Stats enabled="+stats.isStatisticsEnabled());
		stats.setStatisticsEnabled(true);
		System.out.println("Stats enabled="+stats.isStatisticsEnabled());
		
		Session session = sessionFactory.openSession();
		Session otherSession = sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();
		Transaction otherTransaction = otherSession.beginTransaction();
		
		printStats(stats, 0);
		
		Employee emp = (Employee) session.load(Employee.class, 1L);
		printData(emp, stats, 1);
		
		emp = (Employee) session.load(Employee.class, 1L);
		printData(emp, stats, 2);
		
		//clear first level cache, so that second level cache is used
		session.evict(emp);
		emp = (Employee) session.load(Employee.class, 1L);
		printData(emp, stats, 3);
		
		emp = (Employee) session.load(Employee.class, 3L);
		printData(emp, stats, 4);
		
		emp = (Employee) otherSession.load(Employee.class, 1L);
		printData(emp, stats, 5);
		
		//Release resources
		transaction.commit();
		otherTransaction.commit();
		sessionFactory.close();
	}

	private static void printStats(Statistics stats, int i) {
		System.out.println("***** " + i + " *****");
		System.out.println("Fetch Count="
				+ stats.getEntityFetchCount());
		System.out.println("Second Level Hit Count="
				+ stats.getSecondLevelCacheHitCount());
		System.out
				.println("Second Level Miss Count="
						+ stats
								.getSecondLevelCacheMissCount());
		System.out.println("Second Level Put Count="
				+ stats.getSecondLevelCachePutCount());
	}

	private static void printData(Employee emp, Statistics stats, int count) {
		System.out.println(count+":: Name="+emp.getName()+", Zipcode="+emp.getAddress().getZipcode());
		printStats(stats, count);
	}

}
最近下载更多
wycccc  LV1 2019年6月8日
ORO4444  LV12 2019年3月20日
wcy071213  LV27 2018年6月2日
ganfeng  LV2 2018年5月11日
88204879  LV2 2018年3月28日
xiaoyu888  LV5 2018年3月21日
346058210  LV6 2018年1月11日
zmqzmq  LV1 2017年3月23日
yedk123  LV1 2017年2月16日
纸天才  LV1 2017年2月15日
最近浏览更多
wordtong  LV3 2021年6月27日
mudingc木钉  LV30 2021年6月11日
hashjaaa  LV4 2021年1月7日
MARTIN66666  LV1 2020年12月26日
北巷枫  LV9 2020年12月23日
banzhenghao  LV7 2020年11月23日
fmmjjj  LV3 2020年6月26日
jimmyke  LV2 2020年6月16日
IkunLYY  LV4 2020年6月14日
jammes  LV3 2020年6月8日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友