<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

    搜索

    •  

    積分與排名

    • 積分 - 825769
    • 排名 - 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)編碼的處理方式,在對象的初始化的時候被
      調用一次就可以了.
    (2)得到當前的配置信息
      這個方法可以得到當前的配置信息,以便于動態(tài)進行配置參數(shù)的修改.hibernate
      的配置信息只在Hibernate初始化的時候使用一次,在完成初始化之后對配置文件
      或者Configuration對象所做的修改將不會生效.
    (3)得到SessionFactory對象的實例
      這個方法用于得到當前系統(tǒng)運行時的SessionFactory對象的實例,這個對象的實例
      對于整個系統(tǒng)而言是全局唯一的.
    (4)釋放各種資源
      這個方法用于終止Hibernate的報務后,釋放各種Hibernate所使用的資源.雖然這個
      方法幾乎不會用到,但對于申請資源的及時釋放是每個程序應該掌握的基本原則.
    (5)重建SessionFactory
      由于SessionFactory對于整個Hibernate應用是唯一的.并且是在Hibernate初始化
      之后建立好的,而且對于配置文件的修改也不會影響到已經初始化的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
    主站蜘蛛池模板: 色婷五月综激情亚洲综合| 51精品视频免费国产专区| 久久久久se色偷偷亚洲精品av | 免费国产黄线在线观看| 丁香花在线视频观看免费| 美女18毛片免费视频| 亚洲香蕉久久一区二区| 亚洲好看的理论片电影| 精品国产亚洲男女在线线电影 | 国产婷婷高清在线观看免费| 综合在线免费视频| 久久精品一本到99热免费| a级毛片免费高清毛片视频| 日韩大片在线永久免费观看网站| 亚洲kkk4444在线观看| 亚洲精品电影天堂网| 亚洲AV无码不卡在线播放| 国产AⅤ无码专区亚洲AV| 免费大黄网站在线观看| 四虎永久免费地址在线网站| 黄a大片av永久免费| 99久久这里只精品国产免费 | 亚洲黄色激情视频| 亚洲综合一区二区精品久久| 亚洲va在线va天堂va不卡下载| 国产亚洲精aa成人网站| 亚洲日韩在线第一页| 亚洲成av人在片观看| 俄罗斯极品美女毛片免费播放| 日韩免费三级电影| 日本免费中文字幕在线看| 最近2019中文字幕mv免费看| AV免费网址在线观看| 女人毛片a级大学毛片免费| 最近中文字幕无免费视频| 成人影片麻豆国产影片免费观看| 在线观看免费高清视频| 成年人免费观看视频网站| 国产高清免费的视频| 亚洲国产一区视频| 亚洲精品无码av人在线观看 |