【问题标题】:ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml] in project root folderConfigurationException:无法在项目根文件夹中找到 cfg.xml 资源 [hibernate.cfg.xml]
【发布时间】:2016-10-19 04:13:54
【问题描述】:

我在 项目根文件夹中有 hibernate.cfg.xml 文件。

如果我运行的应用程序包含:

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

它在 configure() 方法中崩溃并显示以下消息:

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

输出

Jun 17, 2016 12:04:59 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.0.Final}
Jun 17, 2016 12:04:59 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000205: Loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.h2.Driver, hibernate.service.allow_crawling=false, hibernate.max_fetch_depth=5, hibernate.dialect=org.hibernate.dialect.H2Dialect, hibernate.format_sql=true, hibernate.generate_statistics=true, hibernate.connection.username=sa, hibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;LOCK_TIMEOUT=10000, hibernate.bytecode.use_reflection_optimizer=false, hibernate.connection.password=****, hibernate.connection.pool_size=5}
Jun 17, 2016 12:04:59 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]
    at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:53)
    at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:258)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:244)
    at o.k.j.HibernateTest.main(HibernateTest.java:22)

【问题讨论】:

    标签: hibernate hibernate.cfg.xml


    【解决方案1】:

    如果你使用的是 eclipse 那么 它必须添加到配置中 src\main\java\hibernate.cfg.xml

    【讨论】:

      【解决方案2】:

      hibernate.cfg.xml 文件移动到src/main/resources 解决了我的问题

      【讨论】:

        【解决方案3】:

        必须作为持久化类型添加到配置文件中。

        项目设置 > 构面 > 添加休眠 > 配置路径

        您可以在 View > Tools Windows > Persistence

        中看到它已配置

        GL

        【讨论】:

          【解决方案4】:

          如果您使用 Maven 构建项目,请将“hibernate.cfg.xml”文件放在“src/main/resources”下

          【讨论】:

          • 在 maven clean、build、update 和其他方面花费了一个多小时后,这对我来说就像一个魅力
          • 如何配置 Maven 以在其他地方查找文件?
          • 如果我能给你多于一票,我会的,谢谢
          【解决方案5】:

          我也遇到了类似的问题,我注意到我的休眠配置文件“hibernate.cfg.xml”不在所需的路径上。因此,我将 hibernate.cfg.xml 移动到 src 文件夹中并解决了问题。

          【讨论】:

            【解决方案6】:

            使用 forward / 放置 cfg 文件没有 forward / 它不起作用

            【讨论】:

              【解决方案7】:

              如果您使用 Maven 作为构建工具,请将 hibernate.cfg.xml 放在 resources 文件夹下。

              使应用程序引用 Hibernate 配置文件的步骤。

              右击resources文件夹并选择Mark Directory As选项并点击Resources Root

              这些步骤在 InteliJ IDEA Ultimate 14 中对我有用。

              【讨论】:

                【解决方案8】:

                hibernate.cfg.xml 文件应移至 src 文件夹。

                【讨论】:

                • 所以我无法为我的 hibernate.cfg.xml 文件创建指定文件夹!
                猜你喜欢
                • 2016-06-11
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2021-07-18
                • 1970-01-01
                • 2016-06-14
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多