2

I am attempting to complete this Java Brains tutorial on hibernate, using hibernate3.6.10.Final and Eclipse IDE, and I am encountering an exception that is not covered in the video. Here is the output:

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 com.helo478.firsthibernateproject.SimpleTest.setUpHibernate(SimpleTest.java:31)
    at com.helo478.firsthibernateproject.SimpleTest.main(SimpleTest.java:19)
Caused by: java.lang.NullPointerException
    at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:167)
    at org.hibernate.cfg.Environment.<clinit>(Environment.java:618)
    ... 5 more

[EDIT] I thought the first 3 lines of code were irrelevent, because I've seen a working version of this program (in the tutorial) that had that output. However, it seems that the issue was, indeed with the slf4j jars. Thank you @Jayaram Pradhan and @ drurenia[/EDIT]

I did some searching on StackOverflow already and found this similar case (actually they were probably doing the same tutorial). Unfortunately, that thread does not, at this time, have a usable solution. The questioner solved their problem by starting from scratch and using a different database. I have done that (using both PostgreSQL and MySQL), and I get the same error on each.

The other response indicated that the configuration file should be in the project root directory and that the Configurations.configure() method takes an optional String filepath. I have tried placing the hibernate.cfg.xml file in both the project root directory, as well as the "src" directory. I have also tried pointing to the file in with an argument on the .configure() method. There is no change in output.

Because every line of my Java code is taken directly from the tutorial, I think it is most likely that there is a problem with my hibernate.cfg.xml file. The file is altered from a template that was packaged with hibernate3. I just changed it to reference the MySQL database. Being new to Hibernate, I suppose I must have made a mistake there. Here is the complete text:

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernatedb</property>
        <property name="connection.username">root</property>
        <property name="connection.password">password</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

        <!-- Disable the second-level cache -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <!-- Names the annotated entity class -->
        <mapping class="com.helo478.firsthibernateproject.User" />

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

My question is this:

What, if anything, am I doing wrong in my hibernate.cfg.xml file? If nothing is wrong, what else might explain the failure?

Community
  • 1
  • 1
Don Subert
  • 2,636
  • 4
  • 27
  • 37
  • Can you check if your slf4j dependencies are present in your project? – Diego Urenia Aug 27 '13 at 01:57
  • Thanks for the comment, drurenia. Are you referring to other jars that slf4j depends upon? Where would I find a list of them? It would be cool to get logging working, however I suspect that the logging issue is unrelated to the fatal exception. I have seen, in a video tutorial, a working version of this program that still has broken logging. – Don Subert Aug 27 '13 at 02:58
  • I was talking about the slf4j jars, as @Jayaram Pradhan has pointed out you need *slf4j-api-x.x.x.jar* and *slf4j-simple-x.x.x.jar* in your application – Diego Urenia Aug 27 '13 at 10:34
  • Thank you for your reply. I thought that the problem could not be related to the first 3 lines of output, because I have seen a working version of this program (in the tutorial) that had that output. However, it seems I was wrong. The problem was with the slf4j jars. I appreciate the help – Don Subert Aug 29 '13 at 07:10

1 Answers1

1

I didn't see the tutorial you are reffering but from your stack trace its look like, your slf4j dependencies are not present.

Reffer to the below links which will help you to include the slf4j dependcies:

Hibernate 3.4 with slf4j and log4j

http://www.slf4j.org/manual.html

Make sure you have :

•slf4j-api-1.7.5.jar
•slf4j-simple-1.6.1.jar

in your lib.

Community
  • 1
  • 1
Jayaram
  • 1,715
  • 18
  • 30
  • Thank you for the reply. I had been using slf4j-api-1.6.1.jar and slf4j-simple-1.6.1.jar because that is what was packaged with Hibernate. Per your suggestion, I tried the current version and it seems to work perfectly. 1.6.1 must have been buggy I guess. Thank you so very much for the suggestion. It is very much appreciated. – Don Subert Aug 29 '13 at 06:55
  • 1
    For the record, the final working version used slf4j-api-1.7.5.jar and slf4j-simple-1.7.5.jar thank's again – Don Subert Aug 29 '13 at 07:12