0

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. 
Roam
  • 4,831
  • 9
  • 43
  • 72

2 Answers2

1

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

slf4j-simple-1.6.2.jar
blackpanther
  • 10,998
  • 11
  • 48
  • 78
  • those three "SLF4J:" lines are the exact same in the tutorial i'm following (and running the code fine). They also are the lines i saw in some other boards i saw looking around on internet for an answer to my error. – Roam Jul 03 '13 at 20:30
  • sorry that you didn't enjoy my comment. everything is a java Q. i don't think the Q should be crowded w/that. – Roam Jul 04 '13 at 14:12
0

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?

blackpanther
  • 10,998
  • 11
  • 48
  • 78
dkateros
  • 1,574
  • 10
  • 14
  • hibernate.cfg.xml is in /src folder along with the packages, so it shd be fine(?) i am following a tutorial and the entire code is from there. i don't think i missed anything in the configuration either. – Roam Jul 03 '13 at 20:22
  • i created a "HibernateLib" on Eclipse and put all the Hibernate jars, including Hibernate3.jar into that library. Then i defined "HibernateLib" in the project. in the end, "HibernateLib" is appearing right under the project main folder, in the same level as the JRE System Library and /src folder. – Roam Jul 03 '13 at 20:34
  • version i have here is 3.6.4 in case it matters – Roam Jul 03 '13 at 20:36
  • The line is the same in 3.6.4. Whatever method you use to include the library in eclipse, is it possible that the actual hibernate jars are located in the lib folder of the JRE you use to run your program with in the actual filesystem? – dkateros Jul 03 '13 at 20:47
  • i haven't even stepped foot into jre folder in a while. jre & hibernate libs are two separate libraries on eclipse and defined them without modifying in the project. i used the "build-path" menu and followed the steps in doing these. besides, all these are the way they've been done in that tutorial of mine-- thats what beats me. – Roam Jul 03 '13 at 20:56
  • also, i'm using the editor's feature to include jars into the code. wouldn't be able to locate the jars/classes in their places if something like that were wrong (?) – Roam Jul 03 '13 at 20:57
  • Set up a breakpoint in the line where the NPE is thrown and run the app in debug. – dkateros Jul 03 '13 at 20:58
  • let me see some. havent used the debugger much. – Roam Jul 03 '13 at 21:42
  • Put the debugger results in Q, "EDIT2" – Roam Jul 03 '13 at 22:26
  • it's not seeing the cfg file-- renamed it to make it invisible the way it shd be and getting the same log. will ask that in a new Q. – Roam Jul 03 '13 at 22:37
  • Configure must be dropping the config file just like that when sees (some kind of) an error. see my update on the problem at http://stackoverflow.com/questions/17459273/hibernate-cfg-xml-of-hibernate/17556418#17556418. from what i see, this the only explanation. – Roam Jul 09 '13 at 19:21