hibernateutil.java

来自「分页类,包括中文和英文及其他语言种类,便于二次开发及商业开发,」· Java 代码 · 共 52 行

JAVA
52 行
字号
package com.laoer.comm.db;

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

/**
 * <p>Title: 天乙软件工作室公共包</p>
 * <p>Description: 天乙软件工作室公共包</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: 天乙软件工作室[LAOER.COM/TIANYISOFT.NET]</p>
 * @author 龚天乙(Laoer)
 * @version 1.0
 */

public class HibernateUtil {

  private static final SessionFactory sessionFactory;
  public static final ThreadLocal session = new ThreadLocal();

  static {
    try {
      sessionFactory = new Configuration().configure().buildSessionFactory();
    }
    catch (HibernateException ex) {
      throw new RuntimeException(
          "Exception building SessionFactory: " + ex.getMessage(),
          ex);
    }
  }

  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();
    }
  }

}

⌨️ 快捷键说明

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