hibernateutil.java

来自「sturts好例子」· Java 代码 · 共 49 行

JAVA
49
字号
package com.db;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static Log log = LogFactory.getLog(HibernateUtil.class);
private static final SessionFactory sessionFactory;
public HibernateUtil(){}
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 org.hibernate.Session currentSession() {
	Session s = (Session) session.get();

	if (s == null) {
		s = sessionFactory.openSession();
		session.set(s);
	}
	return s;
}
public static String getInfor(){
	return "this is an information";
}

public static org.hibernate.Session getSession(){
	Session s = (Session) session.get();

	if (s == null) {
		s = sessionFactory.openSession();
		session.set(s);
	}
	return s;
}

public static void closeSession() {
	Session s = (Session) session.get();
	if (s != null)
		s.close();
		session.set(null);
	}
}

⌨️ 快捷键说明

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