原文地址:http://www.hibernate.org/300.html

在Tomcat5.5.x環境下,調用Configuration().addCacheableFile來載入配置,建立Hibernate SessionFactory,成功地提高了載入速度。

推薦你只是在開發階段采用這樣的方式載入,最后的產品發布階段你仍需使用經典的Hibernate.cfg.xml文件,通過Tomcat的ServletContextListener API在應用程序部署的時候建立Hibernate SessionFactory,而不是在程序第一次調用Hiberante的時候。

文件:

net/netbauds/catalina/IHibernateCachableFileLoad.java

這個文件可以在不同的web應用中使用而不用作任何修改。

package ?net.netbauds.catalina;

import ?org.hibernate.cfg.Configuration;

public ? interface ?IHibernateCachableFileLoad?{

????
public ? void ?addMappings(Configuration?conf);

}

net/netbauds/catalina/HibernateSessionFactory.java

使用靜態方法HibernateSessionFactory.getSessionFactory() 來代替我們以前使用的Configuration().configure().buildSessionFactory(),這個方法一般在你的HibernateSession單態類中(參考http://www.hibernate.org/114.html)。

這個文件也可以在不同的應用中使用而不加任何修改:

package ?net.netbauds.catalina;

import ?org.hibernate.SessionFactory;
import ?org.hibernate.cfg.Configuration;

// ?單態的?sessionFactory
public ? class ?HibernateSessionFactory?{
????
private ? static ?SessionFactory?sessionFactory;

????
public ? static ?SessionFactory?getSessionFactory()?{
????????
// ?不要從?JNDI中獲取SessionFactory,?使用一個靜態的?SessionFactory
???????? if ?(sessionFactory? == ? null )?{
????????????Configuration?conf?
= ? new ?Configuration();

????????????
try ?{

????????????????Class?klass?
= ?Class.forName( " config.HibernateCachableFileLoad " );

????????????????IHibernateCachableFileLoad?hibConf?
= ?(IHibernateCachableFileLoad)?klass.newInstance();

????????????????hibConf.addMappings(conf);

????????????}?
catch ?(ClassNotFoundException?e)?{
????????????????
// ?NOOP
????????????}? catch ?(InstantiationException?e)?{
????????????????
// ?NOOP
????????????}? catch ?(IllegalAccessException?e)?{
????????????????
// ?NOOP
????????????}
?
????????????Configuration?confdone?
= ?conf.configure();

????????????
if (confdone? != ? null )?{
????????????????
// ?Use?default?hibernate.cfg.xml
????????????????sessionFactory? = ?confdone.buildSessionFactory();
????????????}
????????}

????????
return ?sessionFactory;
????}
}


config/HibernateCachableFileLoad.java

這個文件是隨web應用的不同而不同的,你需要修改代碼中的某些部分使其適合你的應用。應該有人知道如何更容易的由class loader獲得WEB-INF/classes的絕對路徑吧,這里我只是把它直接寫入了程序中。

你需要修改如下部分:

* 將你所有的Hibernate映射配置文件(*.hbm.xml)加入到程序中(正如你在Hibernate.cfg.xml中所做的)。

package ?config;

import ?net.netbauds.catalina.IHibernateCachableFileLoad;
import ?org.hibernate.cfg.Configuration;

// ?This?class?is?webapp?specific?and?allow?loading?of?mapping?via
// ??addCachableFile();
public ? class ?HibernateCachableFileLoad? implements ?IHibernateCachableFileLoad?{

????
public ? void ?addMappings(Configuration?conf)?{

????????doFile(conf,?
" com/mydomain/MyClassFile001.hbm.xml " );

????????doFile(conf,?
" com/mydomain/MyClassFile002.hbm.xml " );

????}

????
private ? void ?doFile(Configuration?conf,?String?resPath)?{

????????String?path?
= ? null ;

????????URL?u?
= ? this .getClass().getClassLoader().getResource(resPath);

????????
if (u? != ? null )?{

????????????path?
= ?u.getFile();
????????????
if (path? != ? null )
????????????????conf?
= ?conf.addCacheableFile(path);
????????}

????????
if (path? == ? null ? || ?conf? == ? null )
????????????System.err.println(
" ERROR:?Failed?to?load:? " ? + ?resPath);
????}
}

hibernate.cfg.xml

這將使我們標準的hibernate.cfg.xml發生一些變化。如果你使用的是hibernate-configuration-3.0.dtd(3.0版本),那么你可以不寫入任何mapping元素。

如果你使用的是老版本的dtd,那么你需要在hibernate.cfg.xml中寫入一個mapping元素。

An alternative way maybe to programatically configure the connection.datasource in the HibernateSessionFactory() above and maybe hibernate will allow you to do away with looking and parsing the hibernate.cfg.xml completely and build a working factory with the Configuration you have programatically created.

一個可供選擇的方法是使用編寫java代碼的方式來配置上面的SessionFactory的connection.datasource,也許Hibernate將允許你讀取hibernate.cfg.xml文件并且是用你在程序中創建的Configuration來建立一個sessionFactory。

你需要作如下修改:

* 將 java:comp/env/jdbc/ConfigureMeDS 修改為你自己的數據庫連接信息

那么現在:

<?xml?version="1.0"?encoding="UTF-8"?>
<!DOCTYPE?hibernate-configuration
????PUBLIC?"-//Hibernate/Hibernate?Configuration?DTD//EN"
????"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
>

<hibernate-configuration>
????
<session-factory>
????????
<property?name="connection.datasource">java:comp/env/jdbc/ConfigureMeDS</property>

????????
<!--?Mapping?files?-->
????????
<!--?<mapping?resource="uk/mydomain/Dummy.hbm.xml"/>?-->
????
</session-factory>
</hibernate-configuration>

如果你使用的Hibernate版本需要在hibernate.cfg.xml中至少有一個mapping元素,那么你可能需要用到下面的兩個文件,否則,如上就可以了。

uk/mydomain/Dummy.hbm.xml

<?xml?version="1.0"?encoding="UTF-8"?>
<!DOCTYPE?hibernate-mapping?PUBLIC
??????????"-//Hibernate/Hibernate?Mapping?DTD?3.0//EN"
??????????"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
<hibernate-mapping>
????
<class?name="uk.mydomain.Dummy"?table="dummy">
???????
<id?name="id"?type="long"?column="id">
???????????
<generator?class="native"?/>
???????
</id>
????
</class>
</hibernate-mapping>

uk/mydomain/Dummy.java

package?uk.mydomain;

public?class?Dummy?{
????private?long?id;
????private?long?getId()?{
????????return?id;
????}

????private?void?setId(long?id)?{
????????this.id?=?id;
????}
}