我正在使用hibernate 4和Maven:

所以问题是,当我启动服务器时,我看不到它解析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>发布于 2013-02-09 06:53:52
根据我们的设置,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 jars (+它的dependencies_和数据库驱动程序。
您还需要在web.xml中配置监听器
<listener>
<listener-class>org.mypackage.HibernateListener</listener-class>
</listener>如果您的hibernate.cfg.xml文件有问题,您应该会在启动时看到它们。
https://stackoverflow.com/questions/14774986
复制相似问题