Abhishek Abhishek - 3 months ago 14
Java Question

Getting an HTTP Status 500 - org.hibernate.internal.util.config.ConfigurationException: Specified cfg.xml file does not exist

I am working on a project using Java, Spring MVC, and Hibernate (my IDE is IntelliJ). When I try to access the URL (that makes a simple call to my Oracle database via Hibernate) on my localhost after deploying to Tomcat, I receive the following error:


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


My question: why is my program looking in my apache-tomcat folder? I never specified anything in my code to look in that folder, and all of my tests pass when it comes to using Hibernate.

Approaches taken so far to resolve the problem:


  1. Tried rebuilding my WAR file from scratch

  2. Doing a maven clean, compile, install then redeploying to 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?



None of the approaches listed above worked out for me. I have provided my code below along with my file structure to help:




My SessionFactory method:

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);
}
}





File structure:

enter image description here




Update



This works if I place my Hibernate configuration file in my Tomcat folder.

My tests do fail now as I tried to implement shankarsh15's solution in my SessionFactory method. Here is my setUp function before my tests run:

@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();
}


Updated 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>

Answer

I should not have been manually checking for a hibernate configuration file. Instead, my getSessionFactory() method should have looked like the following:

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());
    }