<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年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    常用鏈接

    留言簿(14)

    隨筆檔案(6)

    文章分類(467)

    文章檔案(423)

    相冊

    收藏夾(18)

    JAVA

    搜索

    •  

    積分與排名

    • 積分 - 829294
    • 排名 - 49

    最新評論

    閱讀排行榜

    評論排行榜

    Hibernate 的工具類

      對于Hibernate 3.1 以前的的版本在實現Hibernate工具類時,需要通過兩個線程
    局部變量來保存與當前進行相對應的Session和事務對象的實例.
      而對于Hibernate 3.1 以后的版本,使用線程局部變量保存Session和事務對象的
    工作就完全不需要自己去實現了,只需在Hibernate.cfg.xml配置文件中增加一個名為
    Current_session_context_class的屬性,并且設置該屬性的值為thread.這樣Hibernate
    就可以自動地使用線程局部變量來保存當前的進程的Session和事務對象了.
      相應地,Hibernate也為其Session對象增加了getTransaction()方法,以便可以隨時
    得到當前的事務并進行提交或者回滾操作.這個方法在以前版本的hibernate中是不存在
    的.
    Hibernate工具類主要包括以下功能:
    (1)Hibernate的初始化操作
      這個功能不放在任何方法中,采用靜態編碼的處理方式,在對象的初始化的時候被
      調用一次就可以了.
    (2)得到當前的配置信息
      這個方法可以得到當前的配置信息,以便于動態進行配置參數的修改.hibernate
      的配置信息只在Hibernate初始化的時候使用一次,在完成初始化之后對配置文件
      或者Configuration對象所做的修改將不會生效.
    (3)得到SessionFactory對象的實例
      這個方法用于得到當前系統運行時的SessionFactory對象的實例,這個對象的實例
      對于整個系統而言是全局唯一的.
    (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>
     * 通過靜態的初始化代碼來讀取Hibernate啟動參數,并初始化
     * <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";

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

        static
        {
            // 從缺省的配置文件創建SessionFactory
            try
            {
             // 創建默認的Configuration對象的實例
             // 如果你不使用JDK 5.0或者注釋請使用new Configuration()
             // 來創建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
                {
                    // 使用靜態變量來保持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;
        }


        /**
         * 使用靜態的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 閱讀(3873) 評論(0)  編輯  收藏 所屬分類: hibernate
    主站蜘蛛池模板: 最近中文字幕国语免费完整| 尤物视频在线免费观看| 最近中文字幕免费完整| 亚洲天天在线日亚洲洲精| 外国成人网在线观看免费视频| 亚洲日本va在线视频观看| 97超高清在线观看免费视频| 久久精品国产亚洲麻豆| a级成人毛片免费视频高清| 亚洲VA中文字幕无码毛片| 最近中文字幕mv免费高清视频8| 精品亚洲成a人片在线观看| 中国xxxxx高清免费看视频| 亚洲人成电影在线观看网| 无码人妻一区二区三区免费手机 | 亚洲成av人片不卡无码| 91免费在线播放| 亚洲熟妇丰满xxxxx| vvvv99日韩精品亚洲| 中国极品美軳免费观看| 亚洲蜜芽在线精品一区| 天天操夜夜操免费视频| 一级毛片无遮挡免费全部| 亚洲av中文无码乱人伦在线r▽| 精品一区二区三区免费毛片爱| youjizz亚洲| 亚洲国产av一区二区三区| 十八禁视频在线观看免费无码无遮挡骂过 | 麻豆69堂免费视频| 亚洲av午夜成人片精品网站 | 九九美女网站免费| 亚洲人成日本在线观看| 免费永久看黄在线观看app| 久久青草免费91线频观看不卡| 亚洲大香伊人蕉在人依线| 国产午夜免费秋霞影院| 精品国产免费一区二区三区香蕉| 亚洲妓女综合网99| 亚洲国产精品无码久久九九 | 成年女人喷潮毛片免费播放| 一出一进一爽一粗一大视频免费的|