【问题标题】:Why I get hibernate.cfg.xml not found even if my xml file is in src/main/resources in maven project?为什么即使我的 xml 文件位于 maven 项目的 src/main/resources 中,我也找不到 hibernate.cfg.xml?
【发布时间】:2016-11-23 13:30:35
【问题描述】:

我正在构建一个基于 maven 的应用程序,我想在其中使用 Hibernate 来处理 DB。我阅读了很多文章,其中写道,当我有 Maven 项目时,我必须将我的 xml 文件放入 src/main/resources。我已经这样做了,但我再次遇到错误。这是我的 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.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">""</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/database</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
        <property name="show_sql">true</property> 
        <property name="connection.pool_size">1</property>

        <mapping class="pojo.Student"></mapping>

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

我将此文件添加到 JavaResources/src/main/resources 中。

这是我的 FirstDemo 课程:

public class DemoFirst {
 public static void main(String[] args) {

        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();

        Student student = new Student();
        student.setFirstName("Bob");
        student.setAge(26);

        session.save(student);
        session.getTransaction().commit();

        session.close();

    }

这是错误:

 Exception in thread "main" java.lang.ExceptionInInitializerError
    at pack.DemoFirst.main(DemoFirst.java:12)
Caused by: org.hibernate.HibernateException: /hibernate.cfg.xml not found
    at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:173)
    at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1953)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1934)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1914)
    at pack.HibernateUtil.<clinit>(HibernateUtil.java:17)
    ... 1 more

【问题讨论】:

  • 当您在 IDE 中运行代码时是这样吗?如果是,是哪个 IDE?
  • 这是我在 Eclipse 中运行应用程序的时候...
  • src/main/resources 目录是否在您的构建路径中声明为源目录?
  • 怎么看?我的文件的路径是 JavaResources/src/main/resources
  • 我面前没有 Eclipse,但我认为您在包资源管理器中右键单击文件并选择“配置构建路径”。完成此操作后,会出现一个源选项卡,可让您说出哪些目录是源目录 AFAIR

标签: java xml hibernate maven


【解决方案1】:

我对 Maven 了解不多,但“通常”你的 hibernate.cfg.xml 应该完全像这样命名,并直接进入你的类文件夹(“输出目录”,因为它在 Eclipse 中命名)

希望对你有帮助?!

【讨论】:

    【解决方案2】:
    Configuration cfg = new Configuration();
    cfg.configure("main/resources/hibernate.cfg.xml");
    SessionFactory sf = cfg.buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    
    Student student = new Student();
    student.setFirstName("Bob");
    student.setAge(26);
    
    session.save(student);
    session.getTransaction().commit();
    
    session.close();
    

    请你试试这个。它可以帮助你。

    【讨论】:

    • /main/resources/hibernate.cfg.xml
    【解决方案3】:

    请将您的 hibernate.cfg.xml 移动到 src/main/webapp/WEB-INF ,它应该可以工作。

    【讨论】:

    • 请清理服务器并进行 Maven 全新安装。它必须工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-09
    • 1970-01-01
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多