hibernate官方文档(介绍hbm.xml配置的)

来源:百度文库 编辑:神马文学网 时间:2024/04/27 13:53:15
http://www.hibernate.org/114.html

If you use Hibernate on Tomcat you don't have to use Tomcat'sJNDI-bound JDBC connections. You can let Hibernate manage the JDBCconnection pool. This works on all versions of Tomcat and is very easyto configure.

First, create a hibernate.cfg.xml or hibernate.propertiesfile, as per documentation (no, property names in cfg.xml don't have tobe prefixed with "hibernate.xxx"):

    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">


    
    
        
        org.hibernate.dialect.HSQLDialect
        org.hsqldb.jdbcDriver
        jdbc:hsqldb:hsql://localhost
        sa
        

        
        3
        5
        1800
    
        
        org.hibernate.cache.NoCacheProvider
        false
        false
        3
    
        
        true
        true
    
        
        create
    
        
        thread

        
        
    
        

    



Now copy this file into your WEB-INF/classes directory ofyour web application. Copy hibernate3.jar into your WEB-INF/libdirectory and with it all required 3rd party libraries (seelib/README.txt in the Hibernate distribution). Don't forget to also copyyour JDBC driver to common/lib or WEB-INF/lib. Neverever copy anything in Tomcat into a global directory outside of yourweb application or you are in classloader hell!

Start Hibernate by building a SessionFactory, as shown here with HibernateUtil.

This listener initializes and closes Hibernate on deployment andundeployment, instead of the first user request hitting the application:

public class HibernateListener implements ServletContextListener ...{

    public void contextInitialized(ServletContextEvent event) ...{
        HibernateUtil.getSessionFactory(); // Just call the static initializer of that class    
    }

    public void contextDestroyed(ServletContextEvent event) ...{
        HibernateUtil.getSessionFactory().close(); // Free all resources
    }
}Add it to your web.xml:


    org.mypackage.HibernateListener