gpt4 book ai didi

java - 获取 HTTP 状态 500 - org.hibernate.internal.util.config.ConfigurationException : Specified cfg. xml 文件不存在

转载 作者:行者123 更新时间:2023-11-28 23:25:48 24 4
gpt4 key购买 nike

我正在使用 Java、Spring MVC 和 Hibernate(我的 IDE 是 IntelliJ)开发一个项目。部署到 Tomcat 后,当我尝试在我的本地主机上访问 URL(通过 Hibernate 对我的 Oracle 数据库进行简单调用)时,我收到以下错误:

HTTP Status 500 - Request processing failed; nested exception is org.hibernate.internal.util.config.ConfigurationException: Specified cfg.xml file [C:\apache-tomcat-7.0.52\bin\src\main\resources\hibernate.cfg.xml] does not exist

我的问题:为什么我的程序会在我的 apache-tomcat 文件夹中查找?我从未在我的代码中指定任何内容来查看该文件夹,并且在使用 Hibernate 时我的所有测试都通过了。

目前为解决问题所采取的方法:

  1. 尝试从头开始重建我的 WAR 文件
  2. 执行 maven 清理、编译、安装,然后重新部署到 Tomcat
  3. org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [/HibernateTest/src/hibernate.cfg.xml]
  4. http://www.mkyong.com/hibernate/how-to-load-hibernate-cfg-xml-from-different-directory/
  5. Location of hibernate.cfg.xml in project?

上面列出的所有方法都不适合我。我在下面提供了我的代码以及我的文件结构以提供帮助:


我的 SessionFactory 方法:

private static SessionFactory getSessionFactory() {
String hibernatePropsFilePath = "src/main/resources/hibernate.cfg.xml";
File hibernatePropsFile = new File(hibernatePropsFilePath);

Configuration configuration = new Configuration();
configuration.configure(hibernatePropsFile);
configuration.addAnnotatedClass(Request.class);

StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());

ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();

SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

return sessionFactory;
}

应用程序.java

@RestController
@RequestMapping("/ourApp")
public class Application {

@RequestMapping(value = "/getRequestResponse", method = RequestMethod.GET, headers = "Accept=application/json")
@ResponseBody
public String returnRequestResponse() {
RequestService requestService = new RequestService();
Request request = requestService.findById(1);
Gson gson = new Gson();
return gson.toJson(request);
}
}

文件结构:

enter image description here


更新

如果我将 Hibernate 配置文件放在我的 Tomcat 文件夹中,这将起作用。

我的测试现在确实失败了,因为我试图在我的 SessionFactory 方法中实现 shankarsh15 的解决方案。这是我的测试运行前的设置函数:

@Before
public void setUp() throws Exception {

Configuration configuration = new Configuration();
configuration.addAnnotatedClass(MyService.class)
.addAnnotatedClass(MyModel.class);
configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
configuration.setProperty("hibernate.connection.driver_class", "oracle.jdbc.OracleDriver");
configuration.setProperty("hibernate.connection.url", "jdbc:oracle:thin:@//servername.mycompany.com:12345/abcdefg");
configuration.setProperty("hibernate.connection.username", "username");
configuration.setProperty("hibernate.connection.password", "password");

sessionFactory = configuration.buildSessionFactory();
session = sessionFactory.openSession();
}

更新的 hibernate.cfg.xml:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:oracle:thin:@//servername.mycompany.com:12345/abcdefg</property>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="connection.username">username</property>
<property name="connection.password">password</property>
<property name="show_sql">true</property>
<mapping class="com.mycompany.project.modelName.model.MyModelClass"/>
</session-factory>
</hibernate-configuration>

最佳答案

不要直接指定 hibernate.cfg.xml 文件,请按如下方式重写代码:

Configuration configuration = new Configuration().configure();
ServiceRegistryBuilder registry = new ServiceRegistryBuilder();
registry.applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry = registry.buildServiceRegistry();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();

只需确保 hibernate.cfg.xml 位于 classpath 的根目录下,在您的情况下是正确的,因为它位于 src/main 下/资源

关于java - 获取 HTTP 状态 500 - org.hibernate.internal.util.config.ConfigurationException : Specified cfg. xml 文件不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37217280/

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