Hibernate没有找到hibernate.cfg.xml

时间:2014-09-26 14:33:16

标签: java hibernate gradle

我用gradle创建了一个java项目,很简单,它是一个任务管理器,我有一个Person类和一个Task类,我想使用hibernate但是,hibernate没有加载hibernate.cfg.xml ,我尝试了很多解决方案,但一切都有效。

我有HibernateUtil.java这样写:

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

}

这是布局:

/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

这是完整的痕迹:

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

我希望你们能帮助我,这让我心烦意乱。 如果您需要更多信息,请告诉我。

1 个答案:

答案 0 :(得分:2)

根据关于Hibernate 4 + Gradle的this教程:

您的resources文件夹应位于main下,而不是java下:

修改

可能,你在构建sessionfactory的过程中遗漏了一些东西:

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

而不是:

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

您必须应用设置以将配置的属性传递给构建器: