Reputation: 95
When i run project on eclipse, errors appear:
Jun 08, 2013 6:11:46 AM org.hibernate.annotations.common.Version
INFO: HCANN000001: Hibernate Commons Annotations
{4.0.2.Final} Jun 08, 2013 6:11:46 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.2.Final} Initial SessionFactory
creation failed. java.lang.ExceptionInInitializerError Exception in
thread "main" java.lang.ExceptionInInitializerError at
HibernateUtil.(HibernateUtil.java:13) at
PublisherDAO.addPublisher(PublisherDAO.java:5) at
BookApp.main(BookApp.java:12) Caused by:
java.lang.ExceptionInInitializerError at
org.hibernate.cfg.Configuration.reset(Configuration.java:309) at
org.hibernate.cfg.Configuration.(Configuration.java:275) at
org.hibernate.cfg.Configuration.(Configuration.java:279) at org.hibernate.cfg.AnnotationConfiguration.(AnnotationConfiguration.java:50)
at HibernateUtil.(HibernateUtil.java:8) ... 2 more Caused
by: java.lang.NullPointerException at
org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:170)
at org.hibernate.cfg.Environment.(Environment.java:221) ...
7 more
And code demo:
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();
}
}
i effort very much, but errors aren't solve.Help me!
Upvotes: 0
Views: 1097
Reputation: 324265
They key part seems to be:
java.lang.NullPointerException
at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:170)
at org.hibernate.cfg.Environment.(Environment.java:221)
This suggests that Hibernate is failing to read your configuration. Something is unexpectedly null. Since you didn't specify your exact Hibernate version I couldn't look at the source to say what, so I suggest that you go and do so. See what Hibernate is doing in your exact Hibernate version on line 170 of ConfigHelper.java
, which should be within the getResourceAsStream
method.
If you're still stuck, add the sources to your debug source path, then run your program under a debugger. Set the debugger to trap on uncaught exceptions (usually the default, but there might be a global exception handler you need to deal with). Or set a breakpoint at the line of interest. Then run the program. When it crashes, look back down the stack to see where it comes into contact with something you wrote.
Upvotes: 1