Reputation: 23
I've created a simple example of a class with Hibernate, but I get the following errors:
Initial SessionFactory creation failed. java.lang.ExceptionInInitializerError
java.lang.ExceptionInInitializerError
at org.hibernate.cfg.Configuration.reset(Configuration.java:249)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:216)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:220)
at it.univaq.mwt.tplabs0.Test.main(Test.java:14)
Caused by: java.lang.NullPointerException
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:167)
at org.hibernate.cfg.Environment.<clinit>(Environment.java:585)
... 4 more
Exception in thread "main" java.lang.NullPointerException
at it.univaq.mwt.tplabs0.Test.main(Test.java:20)
My Test class:
package it.univaq.mwt.tplabs0;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Test {
public static void main(String[] args) {
SessionFactory sessionFactory = null;
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed. " + ex);
ex.printStackTrace();
}
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
System.out.println("Creating element");
Element el = new Element("prova", 1);
session.save(el);
System.out.println("Committing Tx");
session.getTransaction().commit();
System.out.println("Closing Session");
if (session.isOpen())
session.close();
}
}
My Hibernate configuration file:
<?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 name="">
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.password">master</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<property name="hibernate.connection.username">master</property>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hbm2ddl.auto">create</property>
<property name="current_session_context_class">thread</property>
<mapping class="it.univaq.mwt.tplabs0.Element" resource="it/univaq/mwt/tplabs0/Element.hbm.xml"/>
</session-factory>
</hibernate-configuration>
My Libraries:
Launching this simple execution I get the error from above. The database I'm using is Oracle 11g.
What happened?
Upvotes: 2
Views: 10188
Reputation: 874
What your code says :
1. SessionFactory sessionFactory = null;
2. try {
3. sessionFactory = new Configuration().configure().buildSessionFactory();
4. } catch (Throwable ex) {
5. System.err.println("Initial SessionFactory creation failed. " + ex);
6. ex.printStackTrace();
7. }
8.
9. Session session = sessionFactory.getCurrentSession();
// Here you are trying to access the Session which is not even created
You are trying to access the Session which is either not configured in CurrentSessionContext or not created at all.
Replace your code at line number 9 with :
Session session = sessionFactory.openSession();
And you will see yourself through.
Upvotes: 1