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

📄 hibernateutil.java

📁 通过面向对象的对象-关系映射持久化技术
💻 JAVA
字号:
package com.faw_qm.ipa.service;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Interceptor;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.*;
import org.apache.log4j.*;
import com.faw_qm.ipa.bo.*;

/**
 * 基础的Hibernate辅助类,用于Hibernate的配置和启动。
 * <p>
 * 通过静态的初始化代码来读取Hibernate启动参数,并初始化
 * <tt>Configuration</tt>和<tt>SessionFactory</tt>对象。
 * <p>
 *
 * @author Louis
 */
/**
*
* @author Louis
*工具类
*/
public class HibernateUtil {
   //apache commons log使用方法:
   public static Logger log = Logger.getLogger(HibernateUtil.class);

   private static Configuration config;
   private static SessionFactory sessionFactory;

   // Create the initial SessionFactory from the default configuration files
   // 初始化
   static {
       try {
           //初始化配置
           config = new Configuration();
           config.addClass(Item.class);
           config.addClass(UserGroup.class);
           config.addClass(UserInfo.class);

           sessionFactory = config.buildSessionFactory();
           //也可以将上述加载过程放入配置文件,用下面方法加载
           // We could also let Hibernate bind it to JNDI:
           // configuration.configure().buildSessionFactory()
       } catch (Throwable ex) {
           // We have to catch Throwable, otherwise we will miss
           // NoClassDefFoundError and other subclasses of Error
           log.error(ex.getMessage());
           throw new ExceptionInInitializerError(ex);
       }
   }

   /**
    * Returns the SessionFactory used for this static class.
    *
    * @return SessionFactory
    */
   public static SessionFactory getSessionFactory() {
       /* Instead of a static variable, use JNDI:
                        SessionFactory sessions = null;
                        try {
               Context ctx = new InitialContext();
               String jndiName = "java:hibernate/HibernateFactory";
               sessions = (SessionFactory)ctx.lookup(jndiName);
                        } catch (NamingException ex) {
               throw new InfrastructureException(ex);
                        }
                        return sessions;
        */
       return sessionFactory;
   }

   /**
    * Returns the original Hibernate configuration.
    *
    * @return Configuration
    */
   public static Configuration getConfiguration() {
       return config;
   }

   /**
    * Rebuild the SessionFactory with the static Configuration.
    *
    */
   public static void rebuildSessionFactory() throws Exception {
       synchronized (sessionFactory) {
           try {
               sessionFactory = getConfiguration().buildSessionFactory();
           } catch (Exception ex) {
               log.error(ex.getMessage());
               //throw DatastoreException.datastoreError(ex);
           }
       }
   }

   /**
    * Rebuild the SessionFactory with the given Hibernate Configuration.
    *
    * @param cfg
    */
   public static void rebuildSessionFactory(Configuration cfg) throws
           Exception {
       synchronized (sessionFactory) {
           try {
               sessionFactory = cfg.buildSessionFactory();
               config = cfg;
           } catch (Exception ex) {
               log.error(ex.getMessage());
               //throw DatastoreException.datastoreError(ex);

           }
       }
   }

   /**
    * 获取一个临时Session
    * @return
    * @throws DatastoreException
    */
   public static Session getSession() throws Exception {
       try {
           return sessionFactory.openSession();

       } catch (Exception ex) {
           log.error(ex.getMessage());
           throw new Exception(ex);
       }
   }

   /**
    * 关闭工厂
    */
   public static void close() {
       try {
           sessionFactory.close();
       } catch (Exception ex) {
           log.error(ex.getMessage());

       }
   }

   /**
    * 关闭临时session
    * @param session
    * @throws DatastoreException
    */
   public static void closeSession(Session session) throws Exception {
       try {
           session.close();
       } catch (Exception ex) {
           log.error(ex.getMessage());
           //throw DatastoreException.datastoreError(ex);
       }
   }

   /**
    * 事务回滚,当操作数据库失败时调用
    * @param transaction
    * @throws DatastoreException
    */

   public static void rollbackTransaction(Transaction transaction) throws
           Exception {
       try {
           if (transaction != null) {
               transaction.rollback();
           }
       } catch (Exception ex) {
           log.error(ex.getMessage());
           //throw DatastoreException.datastoreError(ex);
       }
   }
}

⌨️ 快捷键说明

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