【问题标题】:Hibernate DTD not loading from the classpath, the jar is there, still getting FileNotFoundExceptionHibernate DTD 没有从类路径加载,jar 在那里,仍然得到 FileNotFoundException
【发布时间】:2014-12-05 11:35:44
【问题描述】:

我来了

Caused by:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException:
Configuration problem: Failed to import bean definitions from relative
location [hibernate.cfg.xml] Offending resource: ServletContext
resource [/WEB-INF/applicationContext.xml]; nested exception is
org.springframework.beans.factory.BeanDefinitionStoreException:
IOException parsing XML document from ServletContext resource
[/WEB-INF/hibernate.cfg.xml]; nested exception is
java.io.FileNotFoundException: class path resource
[org/hibernate/hibernate-configuration-3.0.dtd] cannot be opened
because it does not exist

这是我正在使用的依赖项

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.5.Final</version>
        </dependency>

hibernate.cfg.xml -

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration SYSTEM
    "classpath://org/hibernate/hibernate-configuration-3.0.dtd">

在spring中使用import导入hibernate文件-

<import resource="hibernate.cfg.xml"/>

Jar 存在于 maven 依赖项中,但仍无法加载 DTD

【问题讨论】:

  • 为什么不直接删除 DOCTYPE?它是否定义实体?
  • 只想提到XML Catalogs 作为外部DTD 的替代品。 - "classpath:///org..." 或单个 /?
  • 你试过&lt;!DOCTYPE hibernate-configuration SYSTEM "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt; 吗?这也适用于离线。

标签: java xml spring hibernate configuration


【解决方案1】:

您不需要在 Spring 应用程序上下文中导入 hibernate.cfg.xml

所以,而不是:

<import resource="hibernate.cfg.xml"/>

您需要确保 hibermate.cfg.xml 位于您的 jar 中的某个位置。

如果您使用的是 Maven,只需将其放在以下路径中:

${project.home}/src/main/resources/hibernate.cfg.xml

这样,hibernate.cfg.xml 文件将在构建期间包含在模块 jar 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2017-08-16
    • 1970-01-01
    • 2018-10-11
    • 2018-12-31
    相关资源
    最近更新 更多