1

I made a java project with gradle, so simple, it is a task manager, i have a Person class and a Task class, i want to use hibernate but, hibernate doesn't load the hibernate.cfg.xml, i tried many solutions, but anything has worked.

I have a HibernateUtil.java writen like this:

package org.gradle.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new Configuration().configure().buildSessionFactory(
                new StandardServiceRegistryBuilder().build() );
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}

This is the layout:

/my-project
  /src
    /main
      /java
        /org
          /gradle
            Main.java
            /controllers
              PersonController.java
              TaskController.java
            /domains
              Person.java
              Task.java
              Person.hbm.xml
              Task.hbm.xml
            /util
              HibernateUtil.java
      /java
        /resources
          hibernate.cfg.xml
          /org
            /gradle
    /test
      /java
        /org
          /gradle
            PersonTest.java
            TaskTest.java

And this is the full trace:

Sep 26, 2014 11:32:11 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
Sep 26, 2014 11:32:11 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.6.Final}
Sep 26, 2014 11:32:11 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Sep 26, 2014 11:32:11 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Sep 26, 2014 11:32:11 AM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Sep 26, 2014 11:32:11 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not found
Exception in thread "main" java.lang.ExceptionInInitializerError
    at org.gradle.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:20)
    at org.gradle.util.HibernateUtil.<clinit>(HibernateUtil.java:9)
    at org.gradle.controllers.PersonController.add(PersonController.java:10)
    at org.gradle.Main.main(Main.java:12)
Caused by: org.hibernate.HibernateException: /hibernate.cfg.xml not found
    at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:173)
    at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:2093)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2074)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2054)
    at org.gradle.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:14)
    ... 3 more

I hope you guys could help me, this is breaking my mind. If you need something more, please let me know.

  • 1
    check your classes directory and see if the hinernate.cfg.xml file is copied there at correct path. I am not sure about how gradle works but if it is Maven then the path I have to check is target/classes/hibernate.cfg.xml – Chaitanya Sep 26 '14 at 14:34
  • gradle constructs a new folder over the root dir, named build, so when i run gradle build, the hibernate.cfg.xml is at /my-project/build/resources/main/hibernate.cfg.xml – Vercryger Sep 26 '14 at 14:43
  • put the hibernate.cfg.xml file in src folder – ares Sep 26 '14 at 14:50
  • If you want to double check and see everything is Ok, have a look at this tutorial on hibernate4+gradle:concretepage.com/hibernate-4/… – user3487063 Sep 26 '14 at 14:57
  • That should have worked, don't know what is happening but you can try an alternate one here Configuration config = new Configuration().configure(new File("configfilepath")); – ares Sep 26 '14 at 14:58
2

According to this tutorials on Hibernate 4+Gradle:

your resources folder should be under main, not under java:

Edit:

probably, you are missing something in the way you are building the sessionfactory:

Configuration configuration = new Configuration().configure();
        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
        sessionFactory = configuration.buildSessionFactory(builder.build());

instead of :

return new Configuration().configure().buildSessionFactory(
                new StandardServiceRegistryBuilder().build() );

you have to applysettings to pass properties of your configuration to the builder:

|improve this answer|||||
  • Thanks, but i moved it on main folder, and it still not working with the same error: Caused by: org.hibernate.HibernateException: /hibernate.cfg.xml not found – Vercryger Sep 26 '14 at 15:19
  • you need to pass configuration properties to your builder: StandardServiceRegistryBuilder().applySettings(configuration.getProperties()), see my edited answer – user3487063 Sep 26 '14 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.