简体   繁体   中英

Hibernate can't read hibernate.cfg.xml file

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM