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

标签 java hibernate

我正在尝试完成 this Java Brains tutorial on hibernate ,使用 hibernate3.6.10.Final 和 Eclipse IDE,我遇到了视频中未涵盖的异常。这是输出:

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

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

我已经在 StackOverflow 上做了一些搜索并找到了 this similar case (实际上他们可能在做同样的教程)。不幸的是,该线程目前没有可用的解决方案。提问者通过从头开始并使用不同的数据库解决了他们的问题。我已经这样做了(同时使用 PostgreSQL 和 MySQL),并且我在每个上都得到了相同的错误。

另一个响应表明配置文件应该在项目根目录中,并且 Configurations.configure() 方法采用可选的字符串文件路径。我尝试将 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 依赖项:

Hibernate 3.4 with slf4j and log4j

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

确保你有:

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

在你的库中。

关于java - Hibernate 无法读取 hibernate.cfg.xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18455615/

相关文章:

java - Hibernate/JPA 查询,其中包含列表字段中元素的条件(称为 OneToMany)

java - Java中如何删除特定字符之间的子字符串?

java - Android 设备到 PC 的套接字连接

java - Spring 集成;在复制完成之前复制服务拾取的文件

java - Weblogic如何只有一个消息驱动bean实例/线程

hibernate - 来自 Hibernate Session 的 JPA 样式 Criteria/CriteriaBuilder 查询

hibernate - $Proxy25 无法转换为我的类 Spring 框架

java - Hibernate后端条件查询包含: where(1=1) what does it means?

java - EntityNotFoundException LEFT JOIN JPA - Hibernate

java - 无法访问 FXML 控件