【问题标题】:Hibernate exception on startup启动时休眠异常
【发布时间】:2014-06-16 12:15:46
【问题描述】:

我刚刚开始学习 Hibernate。为此,我编写了一个简单的 Java 程序。 但是当我尝试执行程序时,我遇到了以下异常。

Apr 30, 2014 1:54:18 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
Apr 30, 2014 1:54:18 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.5.Final}
Exception in thread "main" java.lang.ExceptionInInitializerError
    at org.hibernate.cfg.Configuration.reset(Configuration.java:324)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:289)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:293)
    at com.hiber.main.Main.main(Main.java:19)
Caused by: java.lang.NullPointerException
    at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:170)
    at org.hibernate.cfg.Environment.<clinit>(Environment.java:221)
    ... 4 more

以下是程序:

package com.hiber.main;

import javax.naming.NamingException;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class Main {

    public static void main(String[] args) throws NamingException {
        //SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(new StandardServiceRegistryBuilder().build());

        ServiceRegistry serviceRegistry;
        SessionFactory sessionFactory;

        Configuration configuration = new Configuration();
        configuration.configure("/com/hiber/main/hibernate.cfg.xml");
        serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);

        Session sess = sessionFactory.getCurrentSession();
        sessionFactory.openSession();
        sess.beginTransaction();

        Test1 t = new Test1();
        t.setTest1Ind(1);
        t.setName("name");
        t.setPassword("abcd1234");
        sess.saveOrUpdate(t);

        sess.getTransaction().commit();
        sess.close();


    }

}

下面是cfg xml:

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">root1234</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.default_schema">test</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="current_session_context_class">thread</property>
    </session-factory>
</hibernate-configuration>

我已经包含了所有依赖项。希望我没有错过任何东西。 目前无法添加图片。

请任何人查看代码并考虑一下这里可能出现的问题。 提前致谢。

【问题讨论】:

  • 您说cfg xml,但在您的代码中将其引用为hibernate.cfg.xml。您是否已解压缩生成的 JAR 并检查该文件确实是您的代码所说的位置?

标签: java hibernate jakarta-ee nhibernate-mapping


【解决方案1】:

1 删除项目中的依赖项。 2 添加正确的依赖项(jar)。

【讨论】:

    【解决方案2】:

    在类路径中有 hibernate.cfg.xml 并尝试使用这样的代码

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

    例如,如果您使用 maven,它将位于位置 \src\main\resources\hibernate.cfg.xml

    更新:

    检查您是否有正确的依赖项(jar)。 我的 maven 项目中有这些依赖项

    【讨论】:

    • 我将应用程序作为 Eclipse 项目运行。我也尝试过上面的代码,但仍然是同样的异常。另外,不要错过我在 SRC 文件夹下复制了 cfg 文件的类路径。我希望这足以将它放在类路径中?
    • 如果您保留在 src 文件夹中,请尝试使用 configuration.configure("/hibernate.cfg.xml");
    • 其他选项尝试使用包括驱动器在内的完整路径。例如 C:/Users/jjayarman/Google Drive/Java Trials/hibernate_434/src/main/resources/hibernate.cfg.xml
    • 配置配置 = new Configuration().configure(new File("D:\\gaurav\\softwares\\spring_install_suite\\ws\\Hiber\\src\\hibernate.cfg.xml" ));这也失败了,并给出了同样的异常。
    • 好的,你在类路径中有哪些库(jar)?
    【解决方案3】:

    我想你忘记了 hibernate cfg 中的映射类。

    在标签前添加映射类

    </session-factory>
    

    在您的休眠配置文件中,不要忘记在您的班级添加注释。

    映射如下所示:

    <mapping class="Curriculums"/>
    <mapping class="CurriculumsHasStudentId"/>
    <mapping class="Students"/>
    

    带有注释的 java 类如下所示:

    @Entity
    @Table(name = "students")
    public class Students implements Serializable{
    
    @Id
    @Column(name="studentsNrp", unique=true)
    private String studentsNrp;
    
    @Column(name = "studentsName")
    private String studentsName;
    
    @Column(name = "studentsAddress")
    private String studentsAddress;
    
    @Column(name = "studentsCity")
    private String studentsCity;
    
    @Column(name = "studentsPhone")
    private String studentsPhone;
    

    【讨论】:

    • 是的,我添加了映射类,但仍然是同样的问题。
    • 你的java类上加注解了吗?
    【解决方案4】:

    阅读本教程here

    它是关于使用 Annotation 的一步一步的 Hibernate 映射。

    【讨论】: