gpt4 book ai didi

java - 在使用 hibernate 的 Maven 项目中找不到 hibernate.properties

转载 作者:行者123 更新时间:2023-11-30 10:15:24 25 4
gpt4 key购买 nike

我尝试了很多方法来解决这个错误(使用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

如果你有解决办法,请告诉我

提前致谢....

最佳答案

Could not locate cfg.xml resource [/src/main/java/hibernate.cfg.xml] error

这意味着 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 = 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 会做同样的事情。

关于java - 在使用 hibernate 的 Maven 项目中找不到 hibernate.properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50461301/

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