18

我试图测试休眠配置是否正常工作。我试过但我得到一个错误:

INFO: HHH000206: hibernate.properties not found

为此:我创建:

【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] 一个休眠实用程序类

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)

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

4

7 回答 7

19

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

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

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

您应该改用日志框架。

于 2016-02-23T15:00:01.000 回答
5

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

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

        `sessionFactory = configuration.buildSessionFactory(serviceRegistry);`

        `sessionFactory = configuration.buildSessionFactory();`
于 2013-09-30T04:02:22.457 回答
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),但在我看来它已经太旧了

于 2016-08-25T10:47:07.380 回答
1

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

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

于 2013-09-30T05:34:34.687 回答
0

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

无论如何,我看到你使用了休眠注释,所以请试试这段代码,看看找不到错误属性会发生什么。

try {
        AnnotationConfiguration configuration = new AnnotationConfiguration();          
        sessionFactory = configuration.configure().buildSessionFactory();       
    }
于 2013-09-30T08:37:51.217 回答
0

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

于 2019-03-06T18:14:00.943 回答
0

你可以试试

  1. 确保文件 hibernate.cfg.xml 位于 src/main/resource
  2. 添加 pom.xml

<!-- API, java.xml.bind module -->
  <dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
    <version>2.3.2</version>
  </dependency>

<!-- Runtime, com.sun.xml.bind module -->
<dependency>
  <groupId>org.glassfish.jaxb</groupId>
  <artifactId>jaxb-runtime</artifactId>
  <version>2.3.2</version>
</dependency>
它对我有用

于 2022-03-05T17:15:22.307 回答