Hibernate 3.5 抛出“org.dom4j.DocumentException:文档 http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd 第 1 行错误

Hibernate 3.5 throws "org.dom4j.DocumentException: Error on line 1 of document http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd

我的 Hypersistence Optimizer 代码库中有一些 Hibernate 3.5 测试,现在我遇到了这个奇怪的错误:

org.hibernate.InvalidMappingException: Could not parse mapping document from resource hbm/mapping/association/EagerFetchingManyToOneFetchJoinHbmTest.hbm.xml

    at org.hibernate.cfg.Configuration.addResource(Configuration.java:671)
    at io.hypersistence.optimizer.util.AbstractHypersistenceOptimizerTest.newSessionFactory(AbstractHypersistenceOptimizerTest.java:108)
    at io.hypersistence.optimizer.util.AbstractHypersistenceOptimizerTest.init(AbstractHypersistenceOptimizerTest.java:63)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from input stream
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:610)
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:668)
    ... 25 more
Caused by: org.dom4j.DocumentException: Error on line 1 of document http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd : The markup declarations contained or pointed to by the document type declaration must be well-formed. Nested exception: The markup declarations contained or pointed to by the document type declaration must be well-formed.
    at org.dom4j.io.SAXReader.read(SAXReader.java:482)
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:601)
    ... 26 more

所有这些测试 运行 3 年都很好,所以这可能是最近发生的变化。

问题是由最近在 hibernate.org 网站上设置的 HTTP->HTTPS 重定向引起的:

~# wget http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd
--2022-02-25 20:53:22--  http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd
Resolving www.hibernate.org (www.hibernate.org)... 34.235.198.240, 52.200.142.250
Connecting to www.hibernate.org (www.hibernate.org)|34.235.198.240|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://hibernate.org/dtd/hibernate-mapping-3.0.dtd [following]
--2022-02-25 20:53:22--  https://hibernate.org/dtd/hibernate-mapping-3.0.dtd
Resolving hibernate.org (hibernate.org)... 185.199.111.153, 185.199.110.153, 185.199.108.153, ...
Connecting to hibernate.org (hibernate.org)|185.199.111.153|:443... connected.

问题是 Hibernate 3.5 没有正确处理这个问题。

所以,解决方法很简单。

更改 HBM 文件开头的 DTD DOCTYPE 定义:

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

至:

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">