获取 HTTP 状态 500 - org.hibernate.internal.util.config.ConfigurationException: 指定的 cfg.xml 文件不存在
Getting an HTTP Status 500 - org.hibernate.internal.util.config.ConfigurationException: Specified cfg.xml file does not exist
我正在使用 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 时我的所有测试都通过了。
目前为解决问题所采取的方法:
- 尝试从头开始重建我的 WAR 文件
- 执行 Maven 清理、编译、安装然后重新部署到 Tomcat
- http://www.mkyong.com/hibernate/how-to-load-hibernate-cfg-xml-from-different-directory/
- Location of hibernate.cfg.xml in project?
None 上面列出的方法对我有用。我在下面提供了我的代码以及我的文件结构以提供帮助:
我的 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;
}
Application.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 的解决方案。这是我测试前的设置函数 运行:
@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/resources 下。
您是否尝试过为您的配置文件指定完整路径?或者尝试不指定 src 例如。 main/resources/.... 为什么它在您的 tomcat 文件夹中搜索而不是在您的源代码中搜索?
我不应该手动检查休眠配置文件。相反,我的 getSessionFactory()
方法应该如下所示:
private static SessionFactory getSessionFactory() {
Configuration configuration = new Configuration().configure();
configuration.addAnnotatedClass(MyClassNameHere.class);
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties());
return configuration.buildSessionFactory(builder.build());
}
我正在使用 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 时我的所有测试都通过了。
目前为解决问题所采取的方法:
- 尝试从头开始重建我的 WAR 文件
- 执行 Maven 清理、编译、安装然后重新部署到 Tomcat
- http://www.mkyong.com/hibernate/how-to-load-hibernate-cfg-xml-from-different-directory/
- Location of hibernate.cfg.xml in project?
None 上面列出的方法对我有用。我在下面提供了我的代码以及我的文件结构以提供帮助:
我的 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;
}
Application.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 的解决方案。这是我测试前的设置函数 运行:
@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/resources 下。
您是否尝试过为您的配置文件指定完整路径?或者尝试不指定 src 例如。 main/resources/.... 为什么它在您的 tomcat 文件夹中搜索而不是在您的源代码中搜索?
我不应该手动检查休眠配置文件。相反,我的 getSessionFactory()
方法应该如下所示:
private static SessionFactory getSessionFactory() {
Configuration configuration = new Configuration().configure();
configuration.addAnnotatedClass(MyClassNameHere.class);
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties());
return configuration.buildSessionFactory(builder.build());
}