netbeans Hibernate:我的文件夹结构是正确的,但当运行应用程序时,它仍然无法在资源文件夹中找到hibernate.config.xml文件

gudnpqoy  于 2022-11-10  发布在  其他
关注(0)|答案(1)|浏览(114)

我正在使用Maven、Netbeans和Windows
我在“/src/main/resources/hibernate.cfg.xml”上有我的完全配置的hib.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]

这是我的hibcfg.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...

所以基本上我一直在努力绕过这个问题,但完全没有成功。任何帮助都将非常感激。

disbfnqx

disbfnqx1#

终于解决了!我的解决方案是使用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);

相关问题