繁体   English   中英

Hibernate无法读取hibernate.cfg.xml文件

[英]Hibernate can't read hibernate.cfg.xml file

我正在尝试使用hibernate3.6.10.Final和Eclipse IDE 在hibernate上完成此Java Brains教程 ,并且遇到视频中未涵盖的异常。 这是输出:

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

[编辑]我认为代码的前三行是无关紧要的,因为我已经看到了该程序的有效版本(在本教程中),该版本具有该输出。 但是,似乎问题出在slf4j罐子上。 谢谢@Jayaram Pradhan和@drurenia [/ EDIT]

我已经在StackOverflow上进行了一些搜索,发现了类似的情况 (实际上他们可能在做相同的教程)。 不幸的是,该线程目前没有可用的解决方案。 发问者通过从头开始并使用其他数据库来解决他们的问题。 我已经做到了(同时使用PostgreSQL和MySQL),并且在每一个上我都得到相同的错误。

另一个响应指示配置文件应位于项目根目录中,并且Configurations.configure()方法采用可选的String文件路径。 我尝试将hibernate.cfg.xml文件放置在项目根目录以及“ src”目录中。 我也尝试使用.configure()方法上的参数指向文件。 输出没有变化。

因为我的Java代码的每一行都直接来自本教程,所以我认为hibernate.cfg.xml文件很可能有问题。 该文件是从与hibernate3打包在一起的模板中更改的。 我只是将其更改为引用MySQL数据库。 我是Hibernate的新手,我想我在那里肯定犯了一个错误。 这是全文:

<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>

我的问题是这样的:

如果在hibernate.cfg.xml文件中做错了什么(如果有的话)? 如果没有错误,还有什么可以解释失败的原因?

我没有看到您正在学习的教程,但是从您的堆栈跟踪它的外观,您的slf4j依赖项不存在。

请参考以下链接,这些链接将帮助您包括slf4j相关性:

带有slf4j和log4j的Hibernate 3.4

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

确保您有:

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

在你的lib中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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