当我在 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();
}
}
我非常努力,但错误并没有解决。帮帮我!