java – 离线时无法解析hibernate.cfg.xml

每当我与互联网断开连接,我得到以下例外:

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

这只有当我离线时才会发生。解析配置时,hibernate尝试读取DTD吗?这里有什么根本原因?

这是我的hibernate.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>
Hibernate可以在本地解析DTD(没有网络连接)。

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

升级到Hibernate 3.6.8之后我经历过同样的问题。我在类路径上有多个版本的hibernate3.jar,导致加载旧的不兼容版本的DTD Entity Resolver,该版本仅适用于旧的命名空间(http://hibernate.sourceforge.net/)。作为参考,这里是一个链接到较新的DTD Entity Resolver

我正在使用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>
翻译自:https://stackoverflow.com/questions/4301294/cant-parse-hibernate-cfg-xml-while-offline

转载注明原文:java – 离线时无法解析hibernate.cfg.xml