如何修复错误:“INFO:HHH000206:找不到hibernate.properties”?

时间:2013-09-30 02:57:45

标签: java hibernate

我试图测试hibernate配置是否正常工作。我试过但是我收到了一个错误:

INFO: HHH000206: hibernate.properties not found

要做到这一点:我创建:

[1] hibernate配置文件[using xml]

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory name="">
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.default_schema">explorecalifornia</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="hibernate.format_sql">true</property>
  <property name="hibernate.connection.password">abc123</property>
 </session-factory>
</hibernate-configuration>

[2]一个hibernate实用程序类

public class HibernateUtilities {

    private static SessionFactory sessionFactory;
    private static ServiceRegistry serviceRegistry;

    static{
        try{
            Configuration configuration = new Configuration().configure();

            serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        }

        catch(HibernateException exception){
            System.out.println("Problem creating session factory");
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void setSessionFactory(SessionFactory sessionFactory) {
        HibernateUtilities.sessionFactory = sessionFactory;
    }


}

[3]主程序:

import org.hibernate.Session;

    public class Program {

        public static void main(String[] args) {
            System.out.println("Hibernate");

            Session session = HibernateUtilities.getSessionFactory().openSession();
            session.close();

        }

    }

但是当我运行程序时,我得到了以下内容:

Hibernate
Sep 29, 2013 10:47:15 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
Sep 29, 2013 10:47:15 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.5.Final}
Sep 29, 2013 10:47:15 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Sep 29, 2013 10:47:15 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Sep 29, 2013 10:47:15 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Sep 29, 2013 10:47:15 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Problem creating session factory
Exception in thread "main" java.lang.NullPointerException
    at com.simpleprogrammer.Program.main(Program.java:10)

为了解决这个问题,我尝试了Google并应用了我找到的方法。但我仍然无法解决问题。有人可以帮帮我吗?

6 个答案:

答案 0 :(得分:17)

这只是一条INFO消息,告诉您没有hibernate.properties文件。此属性文件不是必需的,因此不会阻止您的应用程序运行。

如果您想知道导致SessionFactory创建失败的原因,您需要将catch块更改为:

catch(HibernateException exception){
     System.out.println("Problem creating session factory");
     exception.printStackTrace();
}

您应该使用日志框架。

答案 1 :(得分:4)

您的问题是创建会话工厂,您的应用程序发生在HibernateUtilities类中,原因可能是,您无法通过sessionRegistry创建一个sessionfactory创建它hibernate配置类,因为您在hibernate.cfg.xml

中注册了配置

只需在HibernateUtilities类中替换以下代码

        `sessionFactory = configuration.buildSessionFactory(serviceRegistry);`

        `sessionFactory = configuration.buildSessionFactory();`

答案 2 :(得分:3)

在我的情况下,我在tomcat上运行带有JPA注释的 Hibernate 5 ,当我改为 glassfish 4.1 时停止工作

错误:

找不到hibernate.properties

确保: src / main / resources / hibernate.cfg.xml存在

如果你只有hibernate-core的依赖,我使用的是hibernate-annotations和hibernate-common-annotations,而且它正在创造冲突。 hibernate 5不需要这两个,我在某处读过。试着删除;)

之后会出现新错误:

java.lang.NoSuchMethodError:org.jboss.logging.Logger.debugf(Ljava / lang / String; I)V

原因是最早的jboss-logging.jar:“YOUR_GLASSFISH_FOLDER / glassfish / modules”

为什么呢? hibernate 5 依赖于最新版本的jboss-logging,而 glassfish 4 使用最旧的版本,即使您在POM文件中声明了最新版本。其实我正在使用:

org.jboss.logging jboss-logging 3.3.0.Final

然后我下载了它并替换了模块路径中的旧.jar并重新开始工作,我花了2天时间尝试解决这个问题,希望它有助于解决未来的问题= D

我使用此链接来帮助我:https://medium.com/@mertcal/using-hibernate-5-on-payara-cc242212a5d6#.npq2hdprz

在我的情况下,另一个解决方案可以回到最后一个Hibernate 4版本(4.3.11.Final),但在我看来已经太老了

答案 3 :(得分:1)

配置文件必须放在类路径中。确保hibernate.cfg.xml在class-path中。由于您的程序是控制台程序,您需要将hibernate.cfg.xml放在src文件夹中。如果你把它放在其他文件夹中而不是在Confingure.configure(fileName);中指定它 正如Mr.Kark所回答的,你必须删除以下行

serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();

答案 4 :(得分:0)

我认为你缺少一些hibernate库(我猜可能是hibernate-tools.jar)。所以请检查一下。

无论如何,我看到你使用了hibernate注释,所以请尝试使用此代码,看看未找到错误属性会发生什么。

try {
        AnnotationConfiguration configuration = new AnnotationConfiguration();          
        sessionFactory = configuration.configure().buildSessionFactory();       
    }

答案 5 :(得分:0)

session-factory 是休眠状态下的特殊标记,它没有任何名称,因此, 您可以将代码 session-factory name =“” 更改为 session-factory 并尝试运行。