我正在开发一个使用 Java、Spring MVC 和 Hibernate 的项目(我的 IDE 是 IntelliJ)。当我尝试在部署到 Tomcat 后访问本地主机上的 URL(通过 Hibernate 对我的 Oracle 数据库进行简单调用)时,我收到以下错误:
HTTP 状态 500 - 请求处理失败;嵌套异常是 org.hibernate.internal.util.config.ConfigurationException: Specified cfg.xml 文件 [C:\apache-tomcat-7.0.52\bin\src\main\resources\hibernate.cfg.xml] 不存在
我的问题:为什么我的程序在我的 apache-tomcat 文件夹中查找?我从来没有在我的代码中指定任何东西来查看该文件夹,并且在使用 Hibernate 时我的所有测试都通过了。
到目前为止解决问题的方法:
- 尝试从头开始重建我的 WAR 文件
- 做一个 Maven 清理,编译,安装然后重新部署到 Tomcat
- org.hibernate.internal.util.config.ConfigurationException:找不到 cfg.xml 资源 [/HibernateTest/src/hibernate.cfg.xml]
- http://www.mkyong.com/hibernate/how-to-load-hibernate-cfg-xml-from-different-directory/
- hibernate.cfg.xml 在项目中的位置?
上面列出的方法都不适合我。我在下面提供了我的代码以及我的文件结构以提供帮助:
我的 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);
}
}
文件结构:
更新
如果我将 Hibernate 配置文件放在我的 Tomcat 文件夹中,这将有效。
我的测试现在确实失败了,因为我试图在我的 SessionFactory 方法中实现 shankarsh15 的解决方案。这是我的测试运行之前的 setUp 函数:
@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>