不解析hibernate.cfg.xml

时间:2013-02-08 14:42:17

标签: hibernate maven tomcat

我正在使用hibernate 4和Maven:

enter image description here

所以问题是当我启动服务器时,我看不到它正在解析hibernate.cfg.xml 并且该表不是在dataBase中创建的;

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory>
    <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.password">password</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost/mvnodb</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">admin</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="show_sql">true</property>
    <mapping class="tn.onp.mvno.model.Person" ></mapping>
    <mapping class="tn.onp.mvno.model.User" ></mapping>
    <mapping class="tn.onp.mvno.model.Call" ></mapping>
    <mapping class="tn.onp.mvno.model.User" ></mapping>
 </session-factory>

1 个答案:

答案 0 :(得分:3)

根据我们的设置,Hibernate通常是通过构建SessionFactory来启动的。除非你使用某种Spring / JPA集成,否则当你启动tomcat时不会自动发生这种情况。

您可以使用以下侦听器在部署和取消部署时初始化和关闭Hibernate。

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
    }
}

你需要在类路径中使用这个类,以及hibernate jar(+它的dependencies_和你的数据库驱动程序。

您还需要在web.xml中配置侦听器

<listener>
    <listener-class>org.mypackage.HibernateListener</listener-class>
</listener>

如果你的hibernate.cfg.xml文件有问题,你应该在启动时看到它们。

相关问题