简介 目录 评价 推荐
  • 谁能初心不负 2018-08-30
    HibernateSessionFactory.java

    package com.imooc.page;


    import org.hibernate.HibernateException;

    import org.hibernate.Session;

    import org.hibernate.cfg.Configuration;

    import org.hibernate.service.ServiceRegistry;

    import org.hibernate.service.ServiceRegistryBuilder;


    /**

     * Configures and provides access to Hibernate sessions, tied to the

     * current thread of execution.  Follows the Thread Local Session

     * pattern, see {@link http://hibernate.org/42.html }.

     */

    public class HibernateSessionFactory {


        /** 

         * Location of hibernate.cfg.xml file.

         * Location should be on the classpath as Hibernate uses  

         * #resourceAsStream style lookup for its configuration file. 

         * The default classpath location of the hibernate config file is 

         * in the default package. Use #setConfigFile() to update 

         * the location of the configuration file for the current session.   

         */

    private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();

        private static org.hibernate.SessionFactory sessionFactory;

        private static Configuration configuration = new Configuration();

        private static ServiceRegistry serviceRegistry; 


    static {

        try {

    configuration.configure();

    serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();

    sessionFactory = configuration.buildSessionFactory(serviceRegistry);

    } catch (Exception e) {

    System.err.println("%%%% Error Creating SessionFactory %%%%");

    e.printStackTrace();

    }

        }

        private HibernateSessionFactory() {

        }

    /**

         * Returns the ThreadLocal Session instance.  Lazy initialize

         * the <code>SessionFactory</code> if needed.

         *

         *  @return Session

         *  @throws HibernateException

         */

        public static Session getSession() throws HibernateException {

            Session session = (Session) threadLocal.get();


    if (session == null || !session.isOpen()) {

    if (sessionFactory == null) {

    rebuildSessionFactory();

    }

    session = (sessionFactory != null) ? sessionFactory.openSession()

    : null;

    threadLocal.set(session);

    }


            return session;

        }


    /**

         *  Rebuild hibernate session factory

         *

         */

    public static void rebuildSessionFactory() {

    try {

    configuration.configure();

    serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();

    sessionFactory = configuration.buildSessionFactory(serviceRegistry);

    } catch (Exception e) {

    System.err.println("%%%% Error Creating SessionFactory %%%%");

    e.printStackTrace();

    }

    }


    /**

         *  Close the single hibernate session instance.

         *

         *  @throws HibernateException

         */

        public static void closeSession() throws HibernateException {

            Session session = (Session) threadLocal.get();

            threadLocal.set(null);


            if (session != null) {

                session.close();

            }

        }


    /**

         *  return session factory

         *

         */

    public static org.hibernate.SessionFactory getSessionFactory() {

    return sessionFactory;

    }

    /**

         *  return hibernate configuration

         *

         */

    public static Configuration getConfiguration() {

    return configuration;

    }


    }


    1回答·988浏览
  • 慕移动1924555 2017-05-17
    HibernareSessionFactory.java拷贝

    注意:

    // 这个是版本4中的用法

    serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();

    sessionFactory = configuration.buildSessionFactory(serviceRegistry);

    // 这个是版本5中的用法

    serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();


    3回答·1259浏览
  • yuantongxin 2016-04-07
    求大神帮忙解决!!!sun官网不存在了,运行出异常!
    已采纳 呵呵呵呵加呵呵呵 的回答

    <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>

    2回答·1510浏览
数据加载中...
开始学习 免费