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

📄 hibernateutil.java

📁 基于servlet/xml开发的网上书店 下载有需写web.xml配置
💻 JAVA
字号:
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);
           }
         }
}

⌨️ 快捷键说明

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