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

📄 hibernateutil.java

📁 网上购物系统用SSH实现的
💻 JAVA
字号:
package cn.com.tarena.ecport.common.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * <pre>
 * Hibernate的工具类
 * 通过此类可以方便地使用Hibernate来管理持久层
 * </pre>
 * 
 * @author zhouyu 2008-1-16
 */
public class HibernateUtil {
	
	/**
	 * Hibernate的sessionFactory
	 * 通过sessionFactory可以创建Hibernate的session
	 */
	private static final SessionFactory sessionFactory;

	static {
		try {
			// Create the SessionFactory from hibernate.cfg.xml
			sessionFactory = new Configuration().configure()
					.buildSessionFactory();
		} catch (Throwable ex) {
			// Make sure you log the exception, as it might be swallowed
			System.err.println("Initial SessionFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}

	/**
	 * 返回Hibernate的sessionFactory
	 * @return sessionFactory
	 */
	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	/**
	 * <pre>
	 * 获得“当前”Hibernate的Session
	 * “当前”的含义可根据Hibernate配置文件中的
	 * hibernate.current_session_context_class来
	 * 配置,详细请参考Hibernate文档
	 * </pre>
	 * @return “当前”Hibernate的Session
	 */
	public static Session getCurrentSession(){
		return HibernateUtil.getSessionFactory().getCurrentSession();
	}
	
}

⌨️ 快捷键说明

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