Java org.hibernate.MappingNotFoundException:未找到资源:hibernate.cfg.xml
我试图在MYSQL中连接,但在尝试加载项配置下一个资源:hibernate.cfg.xml时出错。并出现下一个错误org.hibernate.MappingNotFoundException:resource:hibernate.cfg.xml未找到 HibernateUtil:Java org.hibernate.MappingNotFoundException:未找到资源:hibernate.cfg.xml,java,xml,hibernate,Java,Xml,Hibernate,我试图在MYSQL中连接,但在尝试加载项配置下一个资源:hibernate.cfg.xml时出错。并出现下一个错误org.hibernate.MappingNotFoundException:resource:hibernate.cfg.xml未找到 HibernateUtil: package util; Imports public class HibernateUtil { private static SessionFactory sessionFactory;
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>
试着把
hibernate.cfg.xml
放在src/main/resources
是的,我的问题解决了。什么魔法?在project.iml中,我有下一行:我不确定IDEA是如何处理其内部文件的,但是hibernate.cfg.xml
需要位于项目的类路径中,以便从代码中可见,并且默认情况下,src/main/java
和src/main/resources
下的所有内容都包含在类路径中
<?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