gpt4 book ai didi

java - org.hibernate.MappingNotFoundException : resource: hibernate. cfg.xml 未找到

转载 作者:行者123 更新时间:2023-12-02 04:56:03 26 4
gpt4 key购买 nike

我尝试在 MYSQL 中连接,但在尝试添加配置下一个资源:hibernate.cfg.xml 时出现错误。并有下一个错误 org.hibernate.MappingNotFoundException:资源:hibernate.cfg.xml 未找到

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 jetty等):

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

最佳答案

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

关于java - org.hibernate.MappingNotFoundException : resource: hibernate. cfg.xml 未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28769877/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com