ZhipSoft.com
              冬去春來(lái)
                  鄭重聲明:本Blog純屬個(gè)人學(xué)習(xí)、工作需要,記錄相關(guān)資料。請(qǐng)不要發(fā)表任何有人身攻擊的言論,謝謝!!www.ZhipSoft.com
          posts - 94,comments - 149,trackbacks - 0

          一、 ???????????? Hibernate 訪問(wèn)數(shù)據(jù)庫(kù)時(shí)加載的過(guò)程

          對(duì)于大多數(shù)使用 Hibernate 的朋友來(lái)說(shuō),通常使用一下方式來(lái)獲得 Configuration 實(shí)例: Configuration configure = new Configuration().configure();

          Hibernate 中, Configuration hibernate 的入口。在實(shí)例化一個(gè) Configuration 的時(shí)候, Hibernate 會(huì)自動(dòng)在環(huán)境變量( classpath )里面查找 Hibernate 配置文件 hibernate.properties 。如果該文件存在,則將該文件的內(nèi)容加載到一個(gè) Properties 的實(shí)例 GLOBAL_PROPERTIES 里面,如果不存在,將打印信息   hibernate.properties not found

          接下來(lái) Hibernate 將所有系統(tǒng)環(huán)境變量( System.getProperties() )也添加到 GLOBAL_PROPERTIES 里面。如果配置文件 hibernate.properties 存在,系統(tǒng)還會(huì)進(jìn)一步驗(yàn)證這個(gè)文件配置的有效性,對(duì)于一些已經(jīng)不支持的配置參數(shù),系統(tǒng)將打印出警告信息。

          默認(rèn)狀態(tài)下 configure() 方法會(huì)自動(dòng)在環(huán)境變量( classpath )下面尋找 Hibernate 配置文件 hibernate.cfg.xml ,如果該文件不存在,系統(tǒng)會(huì)打印如下信息并拋出 HibernateException 異常 : hibernate.cfg.xml not found 如果該文件存在, configure() 方法會(huì)首先訪問(wèn)< session-factory >,并獲取該元素 name 的屬性,如果 name 的屬性非空,將用這個(gè)配置的值來(lái)覆蓋 hibernate.properties hibernate.session_factory_name 的配置的值,從這里我們可以看出, hibernate.cfg.xml 里面的配置信息可以覆蓋 hibernate.properties 的配置信息。

          接下來(lái) configure() 方法訪問(wèn)< session-factory >的子元素,首先將使用所有的< property >元素配置的信息來(lái)覆蓋 hibernate.properties 里面對(duì)應(yīng)的配置信息。

          然后 configure() 會(huì)依次訪問(wèn)以下幾個(gè)元素的內(nèi)容

          mapping
          jcs-class-cache
          jcs-collection-cache
          collection-cache

          其中< mapping >是必不可少的,必須通過(guò)配置< mapping >, configure() 才能訪問(wèn)到我們定義的 java 對(duì)象和關(guān)系數(shù)據(jù)庫(kù)表的映射文件( hbm.xml ),例如:

          mapping resource="Cat.hbm.xml"/

          這樣 configure() 方法利用各種資源就創(chuàng)建了一個(gè) Configuration 實(shí)例。對(duì)于整個(gè)項(xiàng)目來(lái)說(shuō),如果用一個(gè)本地線程來(lái)存放這個(gè) Configuration 實(shí)例,那么整個(gè)項(xiàng)目只需要實(shí)例化一次 Configuration 對(duì)象(注: Configuration 實(shí)例很花費(fèi)時(shí)間),也就提高了項(xiàng)目的效率。

          ?

          二、 ???????????? Hibernate 訪問(wèn)多個(gè)數(shù)據(jù)庫(kù)的配置

          根據(jù)以上所述, configure() 方法默認(rèn)是通過(guò)訪問(wèn) hibernate.cfg.xml 的< mapping >元素來(lái)加載我們提供的 .hbm.xml 文件。我們也可以直接指定 hbm.xml 文件,例如 addClass() 方法可以直接通過(guò)指定 class 來(lái)加載對(duì)應(yīng)的映射文件, hibernate 會(huì)將提供的 class 的全名(包括 package )自動(dòng)轉(zhuǎn)化為文件路徑,還可以用 addFile 方法直接指定映射文件。例如: Configuration configurate = new Configuration().addClass(“Book.class”);
          Configuration configurate = new Configuration().addURL(Configuration.class.getResource ("/Book.hbm.xml"));

          Configuration config = new Configuration().addFile("/Cat.hbm.xml");

          這樣,如果用 xml 配置來(lái)配置多個(gè)數(shù)據(jù)庫(kù)的話,那就寫(xiě)多個(gè)配置文件。這里假設(shè)對(duì)應(yīng)兩個(gè)數(shù)據(jù)庫(kù)(一個(gè)是 MySQL ,一個(gè)是 SQLServer ),我們可以把其 xml 文件定義為“ mysql.cfg.xml ”和“ sqlserver.cfg.xml ”。則用 Configuration 類獲取 SessionFactory 的代碼如下:
          SessionFactory mysqlFactory = new Configuration().configure("/mysql.cfg.xml").buildSessionFactory();
          SessionFactory sqlserverFactory = new Configuration().configure("sqlserver.cfg.xml ").buildSessionFactory();

          如果你用 spring ,多數(shù)據(jù)庫(kù)就更簡(jiǎn)單了,像這段代碼可以完成所有配置:
          <beans>
          <bean id="mysqlDS" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
          <property name="url">
          <value>jdbc:mysql://localhost:3306/test</value>
          </property>
          <property name="driverClassName">
          <value>org.gjt.mm.mysql.Driver</value>
          </property>
          <property name="username">
          <value>root</value>
          </property>
          <property name="password">
          <value>123</value>
          </property>
          </bean>

          <bean id="mysqlFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
          <property name="dataSource">
          <ref local="mysqlDS"/>
          </property>
          <property name="hibernateProperties">
          <props>
          <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
          </props>
          </property>
          <property name="mappingResources">
          <list>
          <value>test.hbm.xml</value>
          </list>
          </property>
          </bean>

          <bean id="sqlserverDS" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
          <property name="url">
          <value>jdbc:odbc:test</value>
          </property>
          <property name="driverClassName">
          <value>sun.jdbc.odbc.JdbcOdbcDriver</value>
          </property>
          <property name="username">
          <value>root</value>
          </property>
          <property name="password">
          <value>123</value>
          </property>
          </bean>

          <bean id="sqlserverFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
          <property name="dataSource">
          <ref local="sqlserverDS"/>
          </property>
          <property name="hibernateProperties">
          <props>
          <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
          </props>
          </property>
          <property name="mappingResources">
          <list>
          <value>test.hbm.xml</value>
          </list>
          </property>
          </bean>
          .......
          </beans>

          以上只是配置 Hibernate 訪問(wèn)多個(gè)數(shù)據(jù)庫(kù)的一般方法, hibernate 還有很多可行的配置,有興趣的讀者可以參考它的 reference



                  本Blog純屬個(gè)人學(xué)習(xí)、工作需要,記錄相關(guān)資料。請(qǐng)不要發(fā)表任何有人身攻擊的言論,謝謝! www.zhipsoft.cn
          posted on 2007-01-03 10:37 ZhipSoft 閱讀(792) 評(píng)論(0)  編輯  收藏 所屬分類: Hibernate

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 英吉沙县| 昭觉县| 库尔勒市| 深水埗区| 霍邱县| 丁青县| 静宁县| 上林县| 咸丰县| 望城县| 邵东县| 汨罗市| 德惠市| 吴桥县| 洛川县| 永城市| 濉溪县| 阳曲县| 德昌县| 报价| 浦县| 巴里| 大足县| 建德市| 威远县| 普洱| 太谷县| 开封县| 桑植县| 剑阁县| 德昌县| 贺州市| 台州市| 安庆市| 宜阳县| 城市| 桑日县| 青浦区| 乌拉特中旗| 津南区| 山阳县|