【问题标题】:Hibernate: My folder structure is correct but when running app it still cant locate hibernate.config.xml file on resources folderHibernate:我的文件夹结构是正确的,但是在运行应用程序时它仍然无法在资源文件夹中找到 hibernate.config.xml 文件
【发布时间】:2022-02-08 14:12:42
【问题描述】:

我正在使用 Maven,在 Netbeans 上,在 Windows 上

我有完整配置的 hibernate.cfg.xml 文件 "/src/main/resources/hibernate.cfg.xml"

我已经三重确认这是一个事实This is my folder structure on Netbeans (picture)

运行时我得到这个:

Exception in thread "main" org.hibernate.internal.util.config.ConfigurationException: 
Could not locate cfg.xml resource [/resources/hibernate.cfg.xml]

这是我的休眠 cfg.xml 文件:

<!-- This file should be referenced in the configure() method!!! -->

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration PUBLIC

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
  <session-factory>
    <! -- url -->
    <property name="hibernate.connection.url">jdbc:oracle:thin:@toshiba-pc:1521:SYSTEM</property>
    
    <! -- username -->
    <property name="hibernate.connection.username">system</property>
    
    <!-- password -->
    <property name="hibernate.connection.password">27111995</property>
    
    <!-- database driver -->
    <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>

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

在我的主课上,相关行如下:

 public static void main(String[] args) {
     Configuration cfg = new Configuration();
     cfg.configure("/resources/hibernate.cfg.xml");
 ...more code...

所以基本上我一直在尝试解决这个问题,但完全没有成功。任何帮助将不胜感激。

【问题讨论】:

    标签: java hibernate maven netbeans


    【解决方案1】:

    终于解决了! 我的解决方案是使用 System.property("user.dir") 方法创建一个新的 File 对象并将其传递给 Configuration 构造函数:

    String roothFolder = System.getProperty("user.dir");
    File hibernateConfigFile = new File
    (roothFolder + "/src/main/resources/hibernate.cfg.xml");
    Configuration cfg = new Configuration();
        cfg.configure(hibernateConfigFile);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-28
      • 2022-11-18
      • 1970-01-01
      • 1970-01-01
      • 2014-08-19
      • 1970-01-01
      • 2019-04-30
      相关资源
      最近更新 更多