0

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>
Community
  • 1
  • 1
abhi
  • 1,760
  • 1
  • 24
  • 40

3 Answers3

0

Instead of directly specifying hibernate.cfg.xml file, Please rewrite your code as follows:

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

Just make sure that hibernate.cfg.xml is at the root of the classpath which in your case is true, since it is under src/main/resources.

shankarsh15
  • 1,947
  • 1
  • 11
  • 16
  • ServiceRegistryBuilder is deprecated. I am currently using Hibernate 5.1.0. Any alternatives to this? – abhi May 18 '16 at 14:06
  • Please try : Configuration configuration = new Configuration(); configuration.configure(); ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); SessionFactory sf = configuration.buildSessionFactory(sr); – shankarsh15 May 18 '16 at 14:12
  • Thanks, but now when I receive a "org.hibernate.UnknownEntityTypeException: Unable to locate persister:" I suspect that it is my setUp() within the JUnit test suite. I posted the code and updated configuration file above. – abhi May 18 '16 at 14:38
0

have you tried specifying full path for your config file? or try without specifying src for eg. main/resources/.... and why it is searching in your tomcat folder instead of searching in your source code?

Yadab
  • 1,767
  • 1
  • 10
  • 16
  • Yes I did it still doesn't work. My question is the same as the one you're asking; why is it searching in my Tomcat folder instead of searching in my source code? :) – abhi May 18 '16 at 14:08
0

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());
    }
abhi
  • 1,760
  • 1
  • 24
  • 40