⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hibernatebase.java

📁 本代码是一个权限管理系统源代码
💻 JAVA
字号:
package com.seavision.PermissionManage.help;



import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;

public class HibernateBase {
	protected static SessionFactory sessionFactory;// 会话工厂,用于创建会话

	protected static Session session;// hibernate会话

	protected static Transaction transaction; // hiberante事务

	public HibernateBase() throws HibernateException {
		this.initHibernate();
	}

	// 帮助方法
	protected void initHibernate() throws HibernateException {

		// 装载配置,构造SessionFactory对象
		sessionFactory = new Configuration().configure().buildSessionFactory();
	}

	/**
	 * 开始一个hibernate事务
	 */
	public static void beginTransaction() throws HibernateException {

		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
	}

	/**
	 * 结束一个hibernate事务。
	 */
	public static void endTransaction(boolean commit) throws HibernateException {

		if (commit) {
			transaction.commit();
		} else {
			// 如果是只读的操作,不需要commit这个事务。
			transaction.rollback();
		}
		session.close();
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -