简体   繁体   中英

Hibernate does not find the hibernate.cfg.xml

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.

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM