0

当我在 Eclipse 上运行项目时,出现错误:

2013 年 6 月 8 日上午 6:11:46 org.hibernate.annotations.common.Version

信息:HCANN000001:Hibernate Commons 注释

{4.0.2.Final} 2013 年 6 月 8 日上午 6:11:46 org.hibernate.Version logVersion

信息:HHH000412:休眠核心 {4.2.2.Final} 初始会话工厂

创建失败。java.lang.ExceptionInInitializerError 中的异常

线程“主”java.lang.ExceptionInInitializerError 在

HibernateUtil.(HibernateUtil.java:13) 在

PublisherDAO.addPublisher(PublisherDAO.java:5) 在

BookApp.main(BookApp.java:12) 原因:

java.lang.ExceptionInInitializerError 在

org.hibernate.cfg.Configuration.reset(Configuration.java:309) 在

org.hibernate.cfg.Configuration.(Configuration.java:275) 在

org.hibernate.cfg.Configuration.(Configuration.java:279) 在 org.hibernate.cfg.AnnotationConfiguration.(AnnotationConfiguration.java:50)

在 HibernateUtil.(HibernateUtil.java:8) ... 2 更多

通过:java.lang.NullPointerException 在

org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:170)

在 org.hibernate.cfg.Environment.(Environment.java:221) ...

还有 7 个

和代码演示:

1)hibernate.cfg.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>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.password">postgres</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/postgres</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.default_schema">public</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <mapping class="Publisher"/>
    </session-factory>
</hibernate-configuration>

2)BookApp.java

import java.util.*;
import org.hibernate.*;
public class BookApp {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("hello world!");
        PublisherDAO dao=new PublisherDAO();
        dao.addPublisher("003", "Manning", "Mit");
    }

}

3)HibernateUtil.java

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateUtil {
    private static final SessionFactory sessionFactory;

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

        }catch(Throwable ex){
            System.err.println("Initial SessionFactory creation failed. "+ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory(){
        return sessionFactory;
    }
}

4)Publisher.java

import javax.persistence.*;
import static javax.persistence.GenerationType.IDENTITY;

@Entity
@Table(name="publisher")
public class Publisher {
    private String code;
    private String publisher_name;
    private String address;

    @Id
    @Column(name="code")
    @GeneratedValue(strategy=IDENTITY)
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }

    @Column(name="publisher_name")
    public String getPublisher_name() {
        return publisher_name;
    }
    public void setPublisher_name(String publisher_name) {
        this.publisher_name = publisher_name;
    }

    @Column(name="address")
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}

5)PublisherDAO.java

import java.util.*;
import org.hibernate.*;
public class PublisherDAO {
    public void addPublisher(String code, String name, String address){
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Publisher publisher=new Publisher();
        publisher.setCode(code);
        publisher.setPublisher_name(name);
        publisher.setAddress(address);
        session.save(publisher);
        session.getTransaction().commit();
    }
}

我非常努力,但错误并没有解决。帮帮我!

4

1 回答 1

1

他们的关键部分似乎是:

java.lang.NullPointerException 
at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:170)
at org.hibernate.cfg.Environment.(Environment.java:221) 

这表明 Hibernate 无法读取您的配置。有些东西意外地为空。由于您没有指定确切的 Hibernate 版本,我无法查看源代码来说明什么,所以我建议您去这样做。在第 170 行查看 Hibernate 在您的确切 Hibernate 版本中所做的事情ConfigHelper.java,这应该在getResourceAsStream方法中。

如果您仍然卡住,请将源代码添加到您的调试源路径,然后在调试器下运行您的程序。将调试器设置为捕获未捕获的异常(通常是默认设置,但您可能需要处理全局异常处理程序)。或者在感兴趣的行设置断点。然后运行程序。当它崩溃时,向下查看堆栈以查看它与您编写的内容接触的位置。

于 2013-06-08T11:32:32.280 回答