org.hibernate.MappingNotFoundException:resource:找不到hibernate.cfg.xml

时间:2015-02-27 16:34:56

标签: java xml hibernate

我试图在MYSQL中连接,并且在尝试添加configure next资源时遇到错误:hibernate.cfg.xml。并有下一个错误org.hibernate.MappingNotFoundException:resource:hibernate.cfg.xml not found

的HibernateUtil:

package util;

   Imports

   public class HibernateUtil {
    private static SessionFactory sessionFactory;
    private static ServiceRegistry serviceRegistry;


    static{
        try {
            Configuration configuration = new Configuration();
            configuration.addResource("hibernate.cfg.xml");
            configuration.configure();
            serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
                    configuration.getProperties()).build();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    } catch (HibernateException he) {
        System.err.println("Error creating Session: " + he);
        throw new ExceptionInInitializerError(he);
    }
}

public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

hibernate.cfg.xml中:

<!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="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://**.**.**.**/land_of_dkl_db</property>
        <property name="connection.username">*****</property>
        <property name="connection.password">****</property>
        <property name="connection.pool_size">10</property>
        <!-- Возможно потом нужно будет поставить MySQLInnoDBDialect диалект -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- Логирование sql запросов в консоль -->
        <property name="show_sql">true</property>
        <!-- Есть недокументированная возможность none вместо validate, но вроде validate нас устраивает -->
        <property name="hbm2ddl.auto">validate</property>
        <!-- Спросить у преподов, что значит Enables autocommit for JDBC pooled connection -->
        <property name="hibernate.connection.autocommit">false</property>
        <property name="current_session_context_class">thread</property>

        <mapping class="DAO.logic.User" />

    </session-factory>
</hibernate-configuration>

ProjectName.iml(我删除了很多垃圾,导入maven码头等):

<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
  <component name="FacetManager">
    <facet type="hibernate" name="Hibernate">
      <configuration>
        <datasource-map />
        <naming-strategy-map />
        <deploymentDescriptor name="hibernate.cfg.xml" url="file://$MODULE_DIR$/hibernate.cfg.xml" />
      </configuration>
    </facet>
  </component>
  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="false">
    <output url="file://$MODULE_DIR$/target/classes" />
    <output-test url="file://$MODULE_DIR$/target/test-classes" />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
      <excludeFolder url="file://$MODULE_DIR$/target" />
    </content>
  </component>
</module>

项目结构:

+project 
++src
+++main
++++java
+++++util
++++++HibernateUtil
++hibernate.cfg.xml
++project.iml

1 个答案:

答案 0 :(得分:2)

尝试将hibernate.cfg.xml放在src/main/resources下。