【问题标题】:Where to put hibernate config file将休眠配置文件放在哪里
【发布时间】:2020-02-05 17:23:56
【问题描述】:

这是我的休眠配置文件。我把它放在 src/main/resources 中,我把它放在 src 中。

但我总是收到此错误:

找不到 cfg.xml 资源 [hibernate.cfg.xml]

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    </session-factory>
</hibernate-configuration>

这就是我编写配置文件的方式。我正在使用弹簧。我刚刚发现,实际上所有这些在春天都是不必要的。你可以自动装配 SessionFactory。

【问题讨论】:

  • 文件是否名为hibernate.cfg.xml
  • 是 hibernate.cfg.xml
  • 是spring boot项目吗?以及哪个 Spring 和 Hibernate 版本?
  • 我正在使用 Spring Framework 4.3.3 和 Hibernate 5 。该项目是几年前建立的。

标签: java hibernate jpa


【解决方案1】:

您可以放置​​: hibernate.cfg.xml 文件到

src/main/resources/hibernate.cfg.xml

或者persistence.xml文件到

src/main/resources/META-INF/persistence.xml

如果您使用 Hibernate 的专有 API,则需要 hibernate.cfg.xml。如果您使用的是 JPA,即 Hibernate EntityManager,则需要 persistence.xml。

【讨论】:

    猜你喜欢
    • 2019-07-01
    • 2011-11-25
    • 2017-11-19
    • 2013-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-30
    • 1970-01-01
    相关资源
    最近更新 更多