java - Hibernate new Configuration() 上的 NullPointerException;

标签 java eclipse hibernate nullpointerexception hibernate-mapping

这是我第一次使用 Eclipse 尝试 Hibernate,以下是我所做的事情:

  1. 创建了一个名为 Student.java 的 Java Bean,如下所示:

    package com.jwt.hibernate;
    
    public class Student {
    
    private long id;
    private String name;
    private String degree;
    private String roll;
    private String phone;
    
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDegree() {
        return degree;
    }
    public void setDegree(String degree) {
        this.degree = degree;
    }
    public String getRoll() {
        return roll;
    }
    public void setRoll(String roll) {
        this.roll = roll;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }   
    
    }
    
  2. 创建了一个映射文件 Student.hbm.xml,如下所示:

<hibernate-mapping>

<class name="com.jwt.hibernate.Student" table="student">

<id column="ID" name="id" type="long" />
<property column="name" name="name" type="string" />
<property column="degree" name="degree" type="string" />
<property column="roll" name="roll" type="string" />
<property column="phone" name="phone" type="string" />

</class>

</hibernate-mapping>

3.创建了 hibernate 配置文件 hibernate.cfg.xml,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<!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.url">jdbc:mysql://localhost:3306/hibernatetutorial</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create </property>

<mapping resource="com/jwt/hibernate/Student.hbm.xml" />

</session-factory>
</hibernate-configuration>
  • 创建了类SimpleTest.java,如下所示:

    package com.jwt.hibernate;
    
     import org.hibernate.Session;
     import org.hibernate.SessionFactory;
     import org.hibernate.Transaction;
     import org.hibernate.cfg.Configuration;
    
     public class SimpleTest {
    
    public static void main(String[] args) {
        Configuration cfg = new Configuration();
        cfg.configure("hibernate.cfg.xml");
    
        SessionFactory factory = cfg.buildSessionFactory();
        Session session = factory.openSession();
    
        Student student = new Student();
        student.setName("Mukesh");
        student.setRoll("101");
        student.setPhone("8888");
        student.setDegree("B.E");
    
        Transaction tx = session.beginTransaction();
        session.save(student);
        System.out.println("Object saved successfully.....!!");
        tx.commit();
    
        session.close();
        factory.close();
    }
    
    }
    
  • 现在,当我尝试运行 SimpleTest 时,出现以下错误:

    **INFO: HHH000412: Hibernate Core {4.3.7.Final}
    Exception in thread "main" java.lang.ExceptionInInitializerError
        at org.hibernate.cfg.Configuration.reset(Configuration.java:326)
        at org.hibernate.cfg.Configuration.<init>(Configuration.java:291)
        at org.hibernate.cfg.Configuration.<init>(Configuration.java:295)
        at com.jwt.hibernate.SimpleTest.main(SimpleTest.java:11)
    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**
    

    我仔细检查并确保所有配置和 jar 文件都已添加到类路径中。所以这不是问题。我真的很感激一些关于可能导致此问题的原因以及如何解决它的见解。

    提前致谢!

    最佳答案

    我建议更新到更高版本的 SLF4J。
    或者
    您的 Hibernate.cfg.xml 不在类路径上。它在哪个文件夹中?

    编辑:

    Caused by: java.lang.NullPointerException
    at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:170)
    

    这是代码中的实际异常,如果您的 Hibernate.cfg.xml 已加载,请检查 SELF4J 版本,不要使用 用户库 获取您的 jar 文件,将所有库放入您的 lib 文件夹中,然后在类路径中配置它们。

    关于java - Hibernate new Configuration() 上的 NullPointerException;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27692798/

    相关文章:

    eclipse - 创建 Acceleo 插件时出错 : failed to create task or type apitooling. apigeneration

    java - Hibernate集合设置列作为索引

    java - 从 View 中 hibernate

    java - 我的 spring-boot 应用程序出现以下错误 "org.thymeleaf.exceptions.TemplateInputException:"

    java - 如何使用 JAXB 编码 Map<String, List<Objects>>

    java - 将文件的各个部分放入 HashMap - Java

    java - 空文本框中提示/消息的选定颜色

    java - 如何使用 Intent 将用户输入数据从一个 Activity 发送到另一个 Activity

    java - HibernateTransactionManager 回滚事务

    java - Spring引导应用程序