Roam
Roam

Reputation: 4949

Hibernate -- object creation error

I've set up and am running Hibernate 3.6 on Eclipse/Juno EE.

My first code is giving me a runtime error on instantiating the class Configuration of HN. So-- to be precise,

SessionFactory aFactory;
Configuration conf; 

are fine & running,

but the line next below

conf=new Configuration();

is throwing java.lang.ExceptionInInitializerError.

The code

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

is nowhere near running.

My hibernate.cfg.xml is as follows:

<?xml version='1.0' encoding='utf-8'?>


<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
    <property name="connection.driver_class">org.postgresql.Driver</property>       
    <property name="connection.url">jdbc:postgresql://localhost:5432/ThisDB</property>  
    <property name="connection.username">postgres</property>
    <property name="connection.password">somePass</property>
    <property name="connection.pool_size">1</property>
    <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>  
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <property name="show_sql">true</property>
    <property name="hbm2ddl.auto">create</property>
    <mapping class="dataObjs.someItems"/>  
</session-factory>
</hibernate-configuration>

I copied the contents of the "!DOCTYPE" tag from a project in the same pack I downloaded-- so it should be fine.

My libraries are all added to the project and are imported in the class.

The code is not giving any such errors on creation of "non-Hibernate" objects.

What am i missing?

New to HN. this my first code.

//=====================================

EDIT: Adding the code & the stacktrace:

package somePaket;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import dataObjs.someItems;

public class firstClass{
public static void main(String[] args) {
    System.out.println("..........see this.........");

    someItems kullanici = new someItems();
    itm.setID(1);
    itm.setType("aaa");

    SessionFactory aFactory;
    Configuration conf=new Configuration();;

    new Configuration();
    new Configuration().configure().buildSessionFactory();
}
}

the full log on Console:

..........see this.........
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.ExceptionInInitializerError
    at org.hibernate.cfg.Configuration.reset(Configuration.java:332)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:298)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:302)
    at somePaket.firstClass.main(firstClass.java:18)
Caused by: java.lang.NullPointerException
    at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:167)
    at org.hibernate.cfg.Environment.<clinit>(Environment.java:618)
    ... 4 more

//=====================

EDIT2:

Traced it in Debugger:

LoggerFactory.singleImplementationSanityCheck()

is throwing the following at its line 216:

FileNotFoundException(Throwable).<init>(String) line: 264. 

Upvotes: 0

Views: 752

Answers (2)

dkateros
dkateros

Reputation: 1594

You need to specify the resource path of your hibernate.cfg.xml

IIRC this is done in the configure() method of Configuration.

Edit: turns out there exists Configuration#configure() with no arguments. I guess this expects the hibernate.cfg.xml to be in the root classpath. Are you sure the resource is in your application classpath?

Edit2:

Looked up Hibernate 3.6.8 source. The NullPointer (if I got the version right) is on the following line

stream = Environment.class.getClassLoader().getResourceAsStream( stripped );

Looks like getClassLoader() returns null. The Class#getClassLoader() contract states that if the class was loaded by the bootstrap classloader this method may return null.

This happens if your jars are in the lib folder of your jre (specifically hibernate-core jar, in this case). Is that the case?

Upvotes: 0

blackpanther
blackpanther

Reputation: 11486

You may have to include your slf4j library with the application:

slf4j-simple-1.6.2.jar

Upvotes: 1

Related Questions