Java 如何修复错误:“INFO: HHH000206: hibernate.properties not found”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19085816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:57:55  来源:igfitidea点击:

how to fix the error: "INFO: HHH000206: hibernate.properties not found"?

javahibernate

提问by adarksun

I tried to test whether the hibernate configuration is working properly. I tried but I got an error:

我试图测试休眠配置是否正常工作。我试过了,但出现错误:

INFO: HHH000206: hibernate.properties not found

To do this: I create:

为此:我创建:

[1] hibernate configuration file [using xml]

【1】hibernate配置文件【使用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] A hibernate utility class

[2] 一个休眠实用程序类

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] The main program:

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

        }

    }

But i got the following thing when I run the program:

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

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)

To solve this I tried Google and apply the ways which i found. but still i could not solve the problem. Can anybody please help me?

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

回答by kark

Your problem is creation session Factoryfor your application it happening in HibernateUtilitiesclass , the reason may be due to , you cannot create a sessionfactory by sessionRegistrycreate it by hibernate Configuration class, because you registered your configuration in hibernate.cfg.xml

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

Just Replace the following code in HibernateUtilities class

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

        `sessionFactory = configuration.buildSessionFactory(serviceRegistry);`

to

        `sessionFactory = configuration.buildSessionFactory();`

回答by Hareesh

The configuration files must be placed in the classpath. Make Sure that the hibernate.cfg.xml is in class-path. As your program is console program, u need to place that hibernate.cfg.xml in src folder. If u have placed it in other folder than specify it in Confingure.configure(fileName);And as answered By Mr.Kark u have to delete following lines

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

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

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

回答by Ha Nguyen

I think you are missing some hibernate libs (maybe hibernate-tools.jarI guess). So please check it.

我认为您缺少一些休眠库(也许hibernate-tools.jar我猜)。所以请检查一下。

Anyway, I saw you using the hibernate annotations, so please try this code and see what happen with error properties not found.

无论如何,我看到您使用了休眠注释,因此请尝试使用此代码并查看未找到错误属性会发生什么情况。

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

回答by Vlad Mihalcea

That's only an INFO message telling that you don't have a hibernate.propertiesfile. This property file is not mandatory and so it's not what prevents your application from working.

这只是一条 INFO 消息,告诉您没有hibernate.properties文件。此属性文件不是强制性的,因此它不会阻止您的应用程序工作。

If you want to know what caused the SessionFactorycreation failure, you need to change your catch block to:

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

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

You should use a logging framework instead.

您应该改用日志记录框架。

回答by Vitorlui

In my case, I was running the Hibernate 5with JPA annotations on tomcat and stop working when I changed to glassfish 4.1

就我而言,我在 tomcat 上运行带有 JPA 注释的Hibernate 5,当我更改为glassfish 4.1时停止工作

The error:

错误:

hibernate.properties not found

未找到 hibernate.properties

Make sure:src/main/resources/hibernate.cfg.xml exists

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

And if you only have the dependency of hibernate-core, I was using hibernate-annotations and hibernate-common-annotations and it was creating conflict. The hibernate 5 doesnt need these two, I had read somewhere.Try to remove ;)

如果你只有 hibernate-core 的依赖,我使用 hibernate-annotations 和 hibernate-common-annotations 并且它会产生冲突。hibernate 5 不需要这两个,我在某处读过。尝试删除;)

After that a new error appears:

之后出现新错误:

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

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

The reason was the oldest jboss-logging.jar at: "YOUR_GLASSFISH_FOLDER/glassfish/modules"

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

Why? The hibernate 5has dependency with the newest version of jboss-logging, and the glassfish 4uses the oldest version even if you declare inside your POM file the newest version. Actually I'm using:

为什么?在休眠5具有与JBoss测井的最新版本,并依赖GlassFish的4种用途,即使你宣布你的POM文件中的最新版本旧版本。实际上我正在使用:

org.jboss.logging jboss-logging 3.3.0.Final

Then I downloaded it and replace the old .jar inside the modules path and it back to work, I spent 2 days trying to solve that and I hope it helps some future issues =D

然后我下载了它并替换了模块路径中的旧 .jar 并且它恢复了工作,我花了 2 天的时间试图解决这个问题,我希望它可以帮助一些未来的问题 =D

I used this link to help me: https://medium.com/@mertcal/using-hibernate-5-on-payara-cc242212a5d6#.npq2hdprz

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

Another solution, in my case, could be back to the last Hibernate 4 version (4.3.11.Final) but it is already too old in my opinion

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

回答by ASHISH KUMAR SINGH

session-factoryis a special tag in hibernate it's have no any name so, You can change your code session-factory name=""to session-factoryand try to run.

session-factory是 hibernate 中的一个特殊标签,它没有任何名称,因此,您可以将代码session-factory name=""更改为session-factory并尝试运行。