【问题标题】:hibernate.properties does not found in maven project using hibernate在使用 hibernate 的 maven 项目中找不到 hibernate.properties
【发布时间】:2018-05-22 06:39:08
【问题描述】:

我尝试了很多方法来解决这个错误(使用 maven 项目)

  1. 我将 hibernate.cfg.xml 文件放在 src 文件夹中然后得到同样的错误(没有找到 hibernate.properties)
  2. 我将 hibernate.cfg.xml 文件放在资源文件夹中然后得到同样的错误(没有找到 hibernate.properties)
  3. 我把 hibernate.cfg.xml 文件放在 webapp/WEB-INF 文件夹然后得到同样的错误(没有找到 hibernate.properties)
  4. 我在hibernate.cfg.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">org.postgresql.Driver</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/testdb</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.password">sql123</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hbm2ddl.auto">create </property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <mapping resource="Employee-hbn.xml" />
    </session-factory>
</hibernate-configuration>
我得到的异常如下:

May 22, 2018 2:28:38 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.3.0.Final}
May 22, 2018 2:28:38 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Could not locate cfg.xml resource [/src/main/java/hibernate.cfg.xml]
error

如果您对此有解决方案,请告诉我

提前谢谢....

【问题讨论】:

  • 您已经为映射“hbm”xml 资源提供了“Employee-hbn.xml”。您可以在这里检查文件名是否正确。将文件名更改为“Employee.hbm.xml”一次。一般来说,“hibernate.cfg.xml”文件应该放在“src/main/resources”路径中
  • 是的,“Employee-hbn.xml”的文件名与我放入 hbn 映射的文件名相同,“hibenate.cfg.xml”也在“src/main/resources”路径中... .
  • 把文件名改成“Employee.hbm.xml”,你还需要支持hibernate的jar文件,比如antlr.jar,hibernate3.jar,和commons jars。等等。请检查所需的jar一次
  • 请在映射中保持类似“com/Test/sampleproject/yourhbmfilename.hbm.xml”的路径
  • 请添加错误的堆栈跟踪。你哪里有这个错误?

标签: java hibernate


【解决方案1】:

找不到 cfg.xml 资源 [/src/main/java/hibernate.cfg.xml] 错误

这意味着Hibernate试图在

中找到hibernate.cfg.xml

/src/main/java/hibernate.cfg.xml

这是一个错误的路径。

你应该:

  1. hibernate.cfg.xml 放入resources 文件夹中。
  2. 使用Configuration configuration = new Configuration().configure() 进行配置。
  3. 刷新项目并检查hibernate.cfg.xml 是否位于build 文件夹的根目录中。

一些解释:

Configuration configuration = new Configuration().configure()

意思相同

Configuration configuration = new Configuration().configure("hibernate.cfg.xml");

这一行告诉 Hibernate 将 hibernate.cfg.xml 定位在类路径的根目录中。例如,如果你构建一个jar,这个文件应该在jar的根目录下。

对于war,类路径的根是WEB-INF/classes

当您将文件放入resources 文件夹时,Maven 会将resources 文件夹的所有内容放到类路径的根目录下(放到jar 的根目录下,或者放到 .所以jar 中不会有resources 文件夹,只有它的内容。

刷新项目时,您的 IDE 也会这样做。

【讨论】:

    【解决方案2】:

    您的 hibernate.cfg.xml 需要在类路径中可见。

    对于将它放在 WEB-INF/classes 中的网络可能会起作用。

    对于非 web 的 maven 项目,您可以使用 /src/main/resources/ 这是默认的 maven 资源文件夹(或者如果您已切换,则使用其他文件夹)。

    同样在你的 pom.xml 中你需要依赖 hibernate-core

    【讨论】:

      猜你喜欢
      • 2011-10-29
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 2014-01-21
      • 2012-03-18
      • 2013-07-02
      • 2015-08-08
      • 2012-08-25
      相关资源
      最近更新 更多