2013-02-08 56 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文件的問題,你應該看到他們在啓動時。