hibernateutil.java

来自「hibernate和struts结合的源码」· Java 代码 · 共 74 行

JAVA
74
字号
/*
 * @(#)AdviceProvKYBO.java	1.0 2005-10-08
 *
 * Copyright 2005 GE-SOFT, Inc. All rights reserved.
 * GE-SOFT PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 * 项目名称:LNCNC 
 * 项目描述:“辽宁省工程项目管理系统”是基于J2EE三层结构,以MVC为技术框架,以
 * Java技术 * 为开发技术的项目开发平台。采用面向对象和组件化的编程思想形成独立
 * 的平台底层组 * 件,方便用户的二次开发。
 *
 * 制作记录: (日期 制作人 操作 描述)
 * 2005-10-08  lvbo   建立   立项可研上报
 */
package com.gesoft.hibernate;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


/**
 * 功能说明  
 * @version 1.0, 2005-10-8
 * @author lvbo
 */
public class HibernateUtil {

	private static Log log = LogFactory.getLog(HibernateUtil.class);

    private static final SessionFactory sessionFactory;

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

    public static final ThreadLocal session = new ThreadLocal();

    public static Session currentSession() 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();
    }
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {

	}

}

⌨️ 快捷键说明

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