java开源文档大全致力于打造中国最大最全的开源文档,它提供了最全面最权威的开源资料,同时为大家提供一个交流的平台,如果您有好的想法,欢迎您投稿.
在Tomcat服务器5.5.x环境下,调用Configuration().addCacheableFile来载入配置,建立Hibernate框架 SessionFactory,成功地提高了载入速度。
推荐你只是在开发阶段采用这样的方式载入,最后的产品发布阶段你仍需使用经典的Hibernate框架.cfg.xml文件,通过Tomcat服务器的ServletContextListener API在应用程序部署的时候建立Hibernate框架 SessionFactory,而不是在程序第一次调用Hiberante的时候。
文件:
net/netbauds/catalina/IHibernate框架CachableFileLoad.java
这个文件可以在不同的web应用中使用而不用作任何修改。
package net.netbauds.catalina;
import org.hibernate.cfg.Configuration;
public interface IHibernate框架CachableFileLoad {
public void addMappings(Configuration conf);
} net/netbauds/catalina/Hibernate框架SessionFactory.java
使用静态方法Hibernate框架SessionFactory.getSessionFactory() 来代替我们以前使用的Configuration().configure().buildSessionFactory(),这个方法一般在你的Hibernate框架Session单态类中(参考http://www.hibernate.org/114.html)。
这个文件也可以在不同的应用中使用而不加任何修改:
package net.netbauds.catalina;
import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration;
// 单态的 sessionFactory public class Hibernate框架SessionFactory { private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() { // 不要从 JNDI中获取SessionFactory, 使用一个静态的 SessionFactory if (sessionFactory == null) { Configuration conf = new Configuration();
try {
Class klass = Class.forName("config.Hibernate框架CachableFileLoad");
IHibernate框架CachableFileLoad hibConf = (IHibernate框架CachableFileLoad) 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/Hibernate框架CachableFileLoad.java
这个文件是随web应用的不同而不同的,你需要修改代码中的某些部分使其适合你的应用。应该有人知道如何更容易的由class loader获得WEB-INF/classes的绝对路径吧,这里我只是把它直接写入了程序中。
你需要修改如下部分:
* 将你所有的Hibernate框架映射配置文件(*.hbm.xml)加入到程序中(正如你在Hibernate框架.cfg.xml中所做的)。 package config;
import net.netbauds.catalina.IHibernate框架CachableFileLoad; import org.hibernate.cfg.Configuration;
// This class is webapp specific and allow loading of mapping via // addCachableFile(); public class Hibernate框架CachableFileLoad implements IHibernate框架CachableFileLoad {
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 Hibernate框架SessionFactory() 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< SPAN>property>
< SPAN>session-factory> < SPAN>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" /> < SPAN>id> < SPAN>class> < SPAN>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; } }
java开源文档研究struts,webwork,spring,tomcat,jboss,lucense,nutch,JUnit,eclipse......,如果您有什么意见,欢迎评论和留言. |