view raw
Abhishek Abhishek - 9 months ago 54
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
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]


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

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

ServiceRegistry serviceRegistry =;

SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

return sessionFactory;

public class Application {

@RequestMapping(value = "/getRequestResponse", method = RequestMethod.GET, headers = "Accept=application/json")
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


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:

public void setUp() throws Exception {

Configuration configuration = new Configuration();
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:@//");
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"
<property name="connection.url">jdbc:oracle:thin:@//</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"/>


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();
        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
        return configuration.buildSessionFactory(;