2013-02-08 113 views
1

我和休眠4和Maven工作:的hibernate.cfg.xml不会被解析

enter image description here

所以问题是,当我启动服务器,我不能看到它解析休眠。 cfg.xml 并且表不在数据库中创建;

的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

你有两个属性'hibernate.connection.password'存在。是否有任何异常在T他服务器日志? – orique 2013-02-08 15:03:36

+0

@orique也不例外 – AmiraGL 2013-02-08 16:26:49

回答

3

根据我们的设置,休眠通常是通过构建一个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 
    } 
} 

你需要有这个类在类路径,与休眠瓶(沿+其dependencies_和你的数据库驱动程序。

您还需要配置监听器在您的网页。 XML

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

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