<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 6  文章 - 129  trackbacks - 0
    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    常用鏈接

    留言簿(14)

    隨筆檔案(6)

    文章分類(467)

    文章檔案(423)

    相冊

    收藏夾(18)

    JAVA

    搜索

    •  

    積分與排名

    • 積分 - 825594
    • 排名 - 49

    最新評論

    閱讀排行榜

    評論排行榜

    Hibernate 的工具類

      對于Hibernate 3.1 以前的的版本在實現(xiàn)Hibernate工具類時,需要通過兩個線程
    局部變量來保存與當前進行相對應的Session和事務對象的實例.
      而對于Hibernate 3.1 以后的版本,使用線程局部變量保存Session和事務對象的
    工作就完全不需要自己去實現(xiàn)了,只需在Hibernate.cfg.xml配置文件中增加一個名為
    Current_session_context_class的屬性,并且設置該屬性的值為thread.這樣Hibernate
    就可以自動地使用線程局部變量來保存當前的進程的Session和事務對象了.
      相應地,Hibernate也為其Session對象增加了getTransaction()方法,以便可以隨時
    得到當前的事務并進行提交或者回滾操作.這個方法在以前版本的hibernate中是不存在
    的.
    Hibernate工具類主要包括以下功能:
    (1)Hibernate的初始化操作
      這個功能不放在任何方法中,采用靜態(tài)編碼的處理方式,在對象的初始化的時候被
      調(diào)用一次就可以了.
    (2)得到當前的配置信息
      這個方法可以得到當前的配置信息,以便于動態(tài)進行配置參數(shù)的修改.hibernate
      的配置信息只在Hibernate初始化的時候使用一次,在完成初始化之后對配置文件
      或者Configuration對象所做的修改將不會生效.
    (3)得到SessionFactory對象的實例
      這個方法用于得到當前系統(tǒng)運行時的SessionFactory對象的實例,這個對象的實例
      對于整個系統(tǒng)而言是全局唯一的.
    (4)釋放各種資源
      這個方法用于終止Hibernate的報務后,釋放各種Hibernate所使用的資源.雖然這個
      方法幾乎不會用到,但對于申請資源的及時釋放是每個程序應該掌握的基本原則.
    (5)重建SessionFactory
      由于SessionFactory對于整個Hibernate應用是唯一的.并且是在Hibernate初始化
      之后建立好的,而且對于配置文件的修改也不會影響到已經(jīng)初始化的SessionFactory
      對象.那么如何才能使修改的配置信息對Hibernate起作用呢.這就需要重建SessionFactory
      對象實例.
    (6)攔截器注冊
      用于注冊攔截器并重建SessionFactory.
    HibenateUtil.java
    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;


    /**
     * 基礎的Hibernate輔助類,用于Hibernate的配置和啟動。
     * <p>
     * 通過靜態(tài)的初始化代碼來讀取Hibernate啟動參數(shù),并初始化
     * <tt>Configuration</tt>和<tt>SessionFactory</tt>對象。
     * <p>
     *
     * @author galaxy
     */
    public class HibernateUtil
    {

        private static Log log = LogFactory.getLog(HibernateUtil.class);

        // 指定定義攔截器屬性名
        private static final String INTERCEPTOR_CLASS = "hibernate.util.interceptor_class";

        // 靜態(tài)Configuration和SessionFactory對象的實例(全局唯一的)
        private static Configuration configuration;
        private static SessionFactory sessionFactory;

        static
        {
            // 從缺省的配置文件創(chuàng)建SessionFactory
            try
            {
             // 創(chuàng)建默認的Configuration對象的實例
             // 如果你不使用JDK 5.0或者注釋請使用new Configuration()
             // 來創(chuàng)建Configuration()對象的實例
                configuration = new Configuration();

                // 讀取hibernate.properties或者hibernate.cfg.xml文件
                configuration.configure();

                // 如果在配置文件中配置了攔截器,那么將其設置到configuration對象中
                String interceptorName = configuration.getProperty(INTERCEPTOR_CLASS);
                if (interceptorName != null)
                {
                    Class interceptorClass =
                            HibernateUtil.class.getClassLoader().loadClass(interceptorName);
                    Interceptor interceptor = (Interceptor)interceptorClass.newInstance();
                    configuration.setInterceptor(interceptor);
                }

                if (configuration.getProperty(Environment.SESSION_FACTORY_NAME) != null)
                {
                    // 讓Hibernate將SessionFacory綁定到JNDI
                    configuration.buildSessionFactory();
                }
                else
                {
                    // 使用靜態(tài)變量來保持SessioFactory對象的實例
                    sessionFactory = configuration.buildSessionFactory();
                }

            }
            catch (Throwable ex)
            {
                // 輸出異常信息
                log.error("Building SessionFactory failed.", ex);
                ex.printStackTrace();
                throw new ExceptionInInitializerError(ex);
            }
        }

        /**
         * 返回原始的Configuration對象的實例
         *
         * @return Configuration
         */
        public static Configuration getConfiguration()
        {
            return configuration;
        }

        /**
         * 返回全局的SessionFactory對象的實例
         *
         * @return SessionFactory
         */
        public static SessionFactory getSessionFactory()
        {
            SessionFactory sf = null;
            String sfName = configuration.getProperty(Environment.SESSION_FACTORY_NAME);
            if ( sfName != null)
            {
                log.debug("Looking up SessionFactory in JNDI.");
                try
                {
                    sf = (SessionFactory) new InitialContext().lookup(sfName);
                }
                catch (NamingException ex)
                {
                    throw new RuntimeException(ex);
                }
            }
            else
            {
                sf = sessionFactory;
            }
            if (sf == null)
                throw new IllegalStateException( "SessionFactory not available." );
            return sf;
        }

        /**
         * 關閉當前的SessionFactory并且釋放所有的資源
         */
        public static void shutdown()
        {
            log.debug("Shutting down Hibernate.");
            // Close caches and connection pools
            getSessionFactory().close();

            // Clear static variables
            configuration = null;
            sessionFactory = null;
        }


        /**
         * 使用靜態(tài)的Configuration對象來重新構建SessionFactory。
         */
         public static void rebuildSessionFactory()
         {
            log.debug("Using current Configuration for rebuild.");
            rebuildSessionFactory(configuration);
         }

        /**
         * 使用指定的Configuration對象來重新構建SessionFactory對象。
         *
         * @param cfg
         */
         public static void rebuildSessionFactory(Configuration cfg)
         {
            log.debug("Rebuilding the SessionFactory from given Configuration.");
            synchronized(sessionFactory)
            {
                if (sessionFactory != null && !sessionFactory.isClosed())
                    sessionFactory.close();
                if (cfg.getProperty(Environment.SESSION_FACTORY_NAME) != null)
                    cfg.buildSessionFactory();
                else
                    sessionFactory = cfg.buildSessionFactory();
                configuration = cfg;
            }
         }

        /**
         * 在當前SessionFactory中注冊一個攔截器
         */
        public static SessionFactory registerInterceptorAndRebuild(Interceptor interceptor)
        {
            log.debug("Setting new global Hibernate interceptor and restarting.");
            configuration.setInterceptor(interceptor);
            rebuildSessionFactory();
            return getSessionFactory();
        }

        /**
         * 獲得攔截器對象
         *
         * @return Interceptor
         */
        public static Interceptor getInterceptor()
        {
            return configuration.getInterceptor();
        }

        /**
         * 提交當前事務,并開始一個新的事務
         */
        public static void commitAndBeginTransaction()
        {
         sessionFactory.getCurrentSession().getTransaction().commit();
         sessionFactory.getCurrentSession().beginTransaction();
        }
    }

     



    posted on 2007-08-29 19:12 Ke 閱讀(3865) 評論(0)  編輯  收藏 所屬分類: hibernate
    主站蜘蛛池模板: 免费a级毛片无码a∨蜜芽试看| 成年轻人网站色免费看| 精品亚洲成AV人在线观看| 久九九精品免费视频| 男男黄GAY片免费网站WWW| 国产亚洲综合成人91精品| 国产乱码免费卡1卡二卡3卡| 国产亚洲漂亮白嫩美女在线| 亚洲AV永久无码精品水牛影视| 日本在线高清免费爱做网站| 菠萝菠萝蜜在线免费视频| 亚洲男人天堂2017| 免费一级肉体全黄毛片| 精品国产污污免费网站| 精品国产成人亚洲午夜福利| 亚洲毛片αv无线播放一区| 成年女人男人免费视频播放 | 亚洲精品午夜在线观看| 亚洲国产精品视频| 很黄很色很刺激的视频免费| 一级毛片免费在线| 亚洲午夜一区二区三区| 亚洲AV综合色区无码一区爱AV| 在线视频免费国产成人| 一级毛片不卡片免费观看| 香蕉97碰碰视频免费| 亚洲精品第一综合99久久| 亚洲AV永久无码区成人网站| 免费女人18毛片a级毛片视频| 91精品成人免费国产片| 成在人线av无码免费高潮水 | 亚洲免费视频在线观看| 羞羞视频免费网站入口| 亚洲国产精品网站久久| 久久久久久亚洲精品中文字幕| 亚洲男女内射在线播放| 免费无码成人AV片在线在线播放| 1000部拍拍拍18勿入免费视频软件| 色www永久免费网站| 日韩在线视频免费| 亚洲欧美在线x视频|