hibernate二级缓存示意

本文通过四个具体示例演示了Hibernate二级缓存的工作原理及如何配置。包括在不同Session间共享缓存数据、如何手动清除缓存以及一级缓存与二级缓存之间的交互方式。

 

import java.io.Serializable;

import org.hibernate.CacheMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import junit.framework.TestCase;

public class CacheLevel2Test extends TestCase {

	/**
	 * 开启两个session,分别调用load
	 */
	public void testCache1() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			Student student = (Student)session.load(Student.class, 1);
			System.out.println("student.name=" + student.getName());
			
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
		
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			//不会发出sql,因为开启了二级缓存,session是共享二级缓存的
			Student student = (Student)session.load(Student.class, 1);
			System.out.println("student.name=" + student.getName());
			
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
		
	}	
	
	/**
	 * 开启两个session,分别调用get
	 */
	public void testCache2() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			Student student = (Student)session.get(Student.class, 1);
			System.out.println("student.name=" + student.getName());
			
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
		
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			//不会发出sql,因为开启了二级缓存,session是共享二级缓存的
			Student student = (Student)session.get(Student.class, 1);
			System.out.println("student.name=" + student.getName());
			
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}	
	
	/**
	 * 开启两个session,分别调用load,在使用SessionFactory清除二级缓存
	 */
	public void testCache3() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			Student student = (Student)session.load(Student.class, 1);
			System.out.println("student.name=" + student.getName());
			
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
		
		//管理二级缓存
		SessionFactory factory = HibernateUtils.getSessionFactory();
		//factory.evict(Student.class);
		factory.evict(Student.class, 1);
		
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			//会发出查询sql,因为二级缓存中的数据被清除了
			Student student = (Student)session.load(Student.class, 1);
			System.out.println("student.name=" + student.getName());
			
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}
	
	
	/**
	 * 一级缓存和二级缓存的交互
	 */
	public void testCache4() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			//仅向二级缓存读数据,而不向二级缓存写数据
			session.setCacheMode(CacheMode.GET);
			Student student = (Student)session.load(Student.class, 1);
			System.out.println("student.name=" + student.getName());
			
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
		
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			//发出sql语句,因为session设置了CacheMode为GET,所以二级缓存中没有数据
			Student student = (Student)session.load(Student.class, 1);
			System.out.println("student.name=" + student.getName());
			
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
		
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			//只向二级缓存写数据,而不从二级缓存读数据
			session.setCacheMode(CacheMode.PUT);
			
			//会发出查询sql,因为session将CacheMode设置成了PUT
			Student student = (Student)session.load(Student.class, 1);
			System.out.println("student.name=" + student.getName());
			
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
		
	}	
}
 

 

【故障诊断】【pytorch】基于CNN-LSTM故障分类的轴承故障诊断研究[西储大学数据](Python代码实现)内容概要:本文介绍了基于CNN-LSTM神经网络模型的轴承故障分类方法,利用PyTorch框架实现,采用西储大学(Case Western Reserve University)公开的轴承故障数据集进行实验验证。该方法结合卷积神经网络(CNN)强大的特征提取能力和长短期记忆网络(LSTM)对时序数据的建模优势,实现对轴承不同故障类型和严重程度的高精度分类。文中详细阐述了数据预处理、模型构建、训练流程及结果分析过程,并提供了完整的Python代码实现,属于典型的工业设备故障诊断领域深度学习应用研究。; 适合人群:具备Python编程基础和深度学习基础知识的高校学生、科研人员及工业界从事设备状态监测与故障诊断的工程师,尤其适合正在开展相关课题研究或希望复现EI级别论文成果的研究者。; 使用场景及目标:① 学习如何使用PyTorch搭建CNN-LSTM混合模型进行时间序列分类;② 掌握轴承振动信号的预处理与特征学习方法;③ 复现并改进基于公开数据集的故障诊断模型,用于学术论文撰写或实际工业场景验证; 阅读建议:建议读者结合提供的代码逐行理解模型实现细节,重点关注数据加载、滑动窗口处理、网络结构设计及训练策略部分,鼓励在原有基础上尝试不同的网络结构或优化算法以提升分类性能。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值