2

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:

  • antlr-2.7.6.jar
  • asm.jar
  • asm-attrs.jar
  • c3p0-0.9.1.jar
  • cglib-2.2.jar
  • commons-collections-3.1.jar
  • commons-logging-1.0.4.jar
  • concurrent-1.3.2.jar
  • connector.jar
  • dom4j-1.6.1.jar
  • ehcache-1.2.3.jar
  • hibernate3.jar
  • jaas.jar
  • javassist.jar
  • jboss-cache.jar
  • jboss-common.jar
  • jboss-jmx.jar
  • jboss-system.jar
  • jdbc2_0-stdext.jar
  • jgroups-2.2.8.jar
  • jta.jar
  • log4j-1.2.15.jar
  • oscache-2.1.jar
  • proxool-0.8.3.jar
  • swarmcache-1.0rc2.jar
  • slf4j-api-1.7.7.jar
  • slf4j-log4j12-1.7.7.jar

Launching this simple execution I get the error from above. The database I'm using is Oracle 11g.

What happened?

2
  • Your SessionFactory object is not getting Created Commented Oct 16, 2014 at 10:36
  • Are you using Eclipse IDE ? Commented Oct 16, 2014 at 10:41

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I replaced Session session = sessionFactory.getCurrentSession(); with Session session = sessionFactory.openSession(); but the error is the same :(
I solved the problem by deleting the user library "hibernate" and loading the libraries in the classpath directly (Java build path --> add external jar)!
Yes, Eclipse seems to have this problem while using User libraries and that's why I asked you about that. Anyway, good to see you through the problem you're facing. Cheers !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.