孙未勤的HibernateUtil

本文介绍了一个用于简化Hibernate操作的实用工具类,该类通过静态初始化块创建SessionFactory,并支持多种业务对象类型的映射,如Category、Customer等。此外,还提供了获取SessionFactory、重建SessionFactory以及获取Session的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package netstore.service;
import netstore.framework.exceptions.*;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;
import org.apache.commons.logging.*;
import netstore.businessobjects.*;

public class HibernateUtil {
       private static Log log = LogFactory.getLog(HibernateUtil.class);
 private static Configuration configuration;
 private static SessionFactory sessionFactory;

 // Create the initial SessionFactory from the default configuration files
 static {
  try {
   configuration = new Configuration();
                        configuration.addClass(Category.class)
                                      .addClass(Customer.class)
                                      .addClass(Item.class)
                                      .addClass(Order.class);

   sessionFactory = configuration.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 configuration;
 }

 /**
  * Rebuild the SessionFactory with the static Configuration.
  *
  */
  public static void rebuildSessionFactory()
  throws DatastoreException {
  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 DatastoreException {
  synchronized(sessionFactory) {
   try {
    sessionFactory = cfg.buildSessionFactory();
    configuration = cfg;
   } catch (Exception ex) {
                            log.error(ex.getMessage());
        throw DatastoreException.datastoreError(ex);

   }
  }
  }

         public static Session getSession()throws DatastoreException{
           try {
            return sessionFactory.openSession();

           }catch(Exception ex){
              log.error(ex.getMessage());
       throw DatastoreException.datastoreError(ex);
           }
         }
         public static void close(){
           try {
            sessionFactory.close();
           }catch(Exception ex){
              log.error(ex.getMessage());

           }
         }

        public static void closeSession(Session session)throws DatastoreException{
           try {
            session.close();
           }catch(Exception ex){
              log.error(ex.getMessage());
       throw DatastoreException.datastoreError(ex);
           }
         }

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


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值