错误!在eclipse上连接Hibernate和Postgresql

时间:2013-06-07 23:37:55

标签: java eclipse hibernate postgresql

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

  

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

     

信息:HCANN000001:Hibernate Commons Annotations

     

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

     

信息:HHH000412:Hibernate Core {4.2.2.Final}初始SessionFactory

     

创作失败。

中的java.lang.ExceptionInInitializerError异常      

线程“main”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)at   org.hibernate.cfg.AnnotationConfiguration。(AnnotationConfiguration.java:50)

     

在HibernateUtil。(HibernateUtil.java:8)... 2更多导致

     

by:java.lang.NullPointerException at

     

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

     

at 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();
    }
}

我非常努力,但错误无法解决。帮助我!

1 个答案:

答案 0 :(得分:1)

他们的关键部分似乎是:

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

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

如果仍然卡住,请将源添加到调试源路径,然后在调试器下运行程序。将调试器设置为捕获未捕获的异常(通常是默认的,但可能需要处理全局异常处理程序)。或者在感兴趣的线上设置断点。然后运行该程序。当它崩溃时,回头看看堆栈,看看它与你写的东西有什么联系。