脱机时无法解析休眠.cfg.xml

2022-09-02 12:18:00

每当我与互联网断开连接时,我都会收到以下异常:

org.hibernate.HibernateException: Could not parse configuration: com/mashlife/resources/hibernate.cfg.xml
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1542)
    at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:1035)
    at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:64)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1476)
    at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:1017)

Caused by: org.dom4j.DocumentException: www.hibernate.org Nested exception: www.hibernate.org
    at org.dom4j.io.SAXReader.read(SAXReader.java:484)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1532)
    ... 45 more

发生在我离线时。休眠在解析配置时是否会尝试读取 DTD?这里的根本原因是什么?

这是我的冬眠.cfg.xml:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/foo</property>
        <property name="connection.username">user</property>
        <property name="connection.password">pass</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- DO NOT Echo all executed SQL to stdout -->
        <property name="show_sql">false</property>

        <!-- Names the annotated entity class -->
        <!--<mapping class="org.hibernate.tutorial.annotations.Event"/>-->

    </session-factory>

</hibernate-configuration>

答案 1

休眠可以在本地解析 DTD(无需网络连接)。

您的 DOCTYPE 正在使用 Hibernate 3.6 的新命名空间 (http://www.hibernate.org/dtd/),因此您的类路径中可能有较旧版本的 Hibernate 库。

升级到Hibernate 3.6.8.Final后,我遇到了同样的问题。我在类路径上有多个版本的hibernate3.jar导致加载了DTD实体解析器的旧不兼容版本,该版本仅适用于旧的命名空间(http://hibernate.sourceforge.net/)。作为参考,下面是指向较新的 DTD 实体解析程序的链接。

我正在使用hibernate3-maven-plugin,它对旧版本的Hibernate有一个传递依赖性,所以我只需要在Hibernate 3.6.8.Final上指定一个插件依赖性。

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
    ...
</configuration>
<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.6.8.Final</version>
    </dependency>
</dependencies>
</plugin>

答案 2

这是不可能的,因为,休眠jar文件也加载了一些dtd内容,但互联网连接速度慢,它是工作的。

(1) 休眠配置文件位置

第一个解决方案是使用类路径在系统中提供 DTD 文件位置。因此,脱机工作的DocType将是;

<!DOCTYPE hibernate-configuration SYSTEM 
    "classpath://org/hibernate/hibernate-configuration-3.0.dtd">

(2)将 SourceForge DTD URL 与 SYSTEM 一起使用

我发现另一个有效的解决方案是当我将DTD URL更改为SourceForge并将声明从PUBLIC更改为SYSTEM时。

因此,如果您的系统处于脱机状态,则下面也将起作用。

<!DOCTYPE hibernate-configuration SYSTEM 
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

休眠脱机工作


推荐