Amira
Amira

Reputation: 3270

hibernate.cfg.xml is not parsed

i'm working with hibernate 4 and Maven :

enter image description here

So the problem is when i start the sever ,i can't see it parsing the hibernate.cfg.xml And the table are not created in the 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>

Upvotes: 1

Views: 215

Answers (1)

ddewaele
ddewaele

Reputation: 22603

Depending on our setup, Hibernate is typically started by building a SessionFactory. Unless you're using some kind of Spring / JPA integration, this does not automatically happen when you start tomcat.

You can use the following listener to initialize and close Hibernate on deployment and undeployment.

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

You'll need to have this class in your classpath, along with the hibernate jars (+ its dependencies_ and your database driver.

You'll also need to configure the listener in your web.xml

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

If there are issues with your hibernate.cfg.xml file, you should see them at startup.

Upvotes: 3

Related Questions