2

Hi i trying to write my first Hibernate program, its giving an error while instantiating the session factory. I'm using hibernate 5.0.4 & java 8 & eclipse Luna SR1 (4.4.1) & oracle 11g.

MainClass is:

public class MainMethod {
public static void main(String[] args) {
    SampleClass s = new SampleClass();
    s.setId(1);
    s.setValue("Value_1");

    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

    Session session = sessionFactory.openSession();
    session.beginTransaction();
    session.save(s);
    session.getTransaction().commit();      
}
}

the hibernate.cfg.xml is

<?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">oracle.jdbc.driver.OracleDriver</property>
  <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:mndb11g</property>
  <property name="hibernate.connection.username">temp_p</property>
  <property name="hibernate.connection.password">temp_p</property>
  <property name="connection.pool_size">1</property>

  <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
  <property name="hibernate.default_schema">temp_p</property>

  <property name="hbm2ddl.auto">create</property>
  <property name="show_sql">true</property>

  <mapping class="com.h.SampleClass.SampleClass"/>

</session-factory>
</hibernate-configuration>

Its giving an exception at

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

The exception stack trace is

    Exception in thread "main" java.lang.NullPointerException
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.getResources(ClassLoaderServiceImpl.java:173)
    at java.util.ServiceLoader$LazyIterator.hasNextService(ServiceLoader.java:348)
    at java.util.ServiceLoader$LazyIterator.hasNext(ServiceLoader.java:393)
    at java.util.ServiceLoader$1.hasNext(ServiceLoader.java:474)
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.loadJavaServices(ClassLoaderServiceImpl.java:324)
    at org.hibernate.integrator.internal.IntegratorServiceImpl.<init>(IntegratorServiceImpl.java:40)
    at org.hibernate.boot.registry.BootstrapServiceRegistryBuilder.build(BootstrapServiceRegistryBuilder.java:213)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:119)
    at com.h.MainMethod.main(MainMethod.java:19)

Can anyone please help to solve this. Thanks in advance.

2 Answers 2

2

This could happen due to several reasons.

  • May be the Entity cannot be found. In your hibernate.cfg.xml file the entity is mentioned as com.h.SampleClass.SampleClass Please double check the class name and the package name.

  • Make sure that the hibernate libraries are added as user libraries. Not system libraries. More info

Hope this helps.

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

2 Comments

yes my package is com.h.SampleClass & my class is SampleClass. & but i've added them as system libraries.. changed to user libraries. Working perfect. thanks :)
This works for me. I created a standalone java project and try to import Hibernate libraries as user libraries but selected 'System libraries' checkbox. Error disappeared after unchecking the 'System libraries' checkbox
0

It sounds like hibernate.cfg.xml is not on your classpath when the code runs. If you are using maven put it in src/main/resources.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.