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

📄 hibernateutil.java

📁 Struts+Hibernate的源码
💻 JAVA
字号:
/*
 * 创建日期 2005-4-10
 *
 * TODO 要更改此生成的文件的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
package com.sonic;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;

/**
 * @author Administrator
 *
 * TODO 要更改此生成的类型注释的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
public class HibernateUtil {
	
	private static Log log=LogFactory.getLog(HibernateUtil.class);
	
	private static final SessionFactory sessionFactory;
	
	static {
		  try{
		  	sessionFactory=new Configuration().configure().buildSessionFactory();
		  } catch (Throwable ex) {
		    log.error("Initial SessionFactory creation failed.",ex);
		    throw new ExceptionInInitializerError(ex);
		  }
		}
	
	public static final ThreadLocal session=new ThreadLocal();
	
	public static Session Session() throws HibernateException {
		  Session s=(Session)session.get();
		  //Open a new Session, if this Thread has none yet
		  if (s==null) {
		    s=sessionFactory.openSession();
		    session.set(s);
		  }
		  return s;
		}
	
	public static void closeSession() throws HibernateException {
		  Session s=(Session) session.get();
		  session.set(null);
		  if (s !=null)
		    s.close();
		  }

}

⌨️ 快捷键说明

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