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

rip*_*234 19 java hibernate

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

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
Run Code Online (Sandbox Code Playgroud)

只有当我离线发生.在解析配置时,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>
Run Code Online (Sandbox Code Playgroud)

小智 19

Hibernate可以在本地解析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>
Run Code Online (Sandbox Code Playgroud)

  • 哦,伙计,你完全摇滚.那只是为我节省了很多时间.恢复到"sourceforge"网址,如"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"也修复了它,因为显然在旧版的jar中找到了*,请参阅https://forum.hibernate.组织/ viewtopic.php?F = 1&T = 943281&开始= 0 (2认同)

小智 7

这是不可能的,因为,hibernate jar文件也加载了一些dtd内容,但它的工作速度很慢.

(1)休眠配置文件位置

第一种解决方案是使用classpath在系统中提供DTD文件位置.所以离线工作的DocType会是;

<!DOCTYPE hibernate-configuration SYSTEM 
    "classpath://org/hibernate/hibernate-configuration-3.0.dtd">
Run Code Online (Sandbox Code Playgroud)

(2)在SYSTEM中使用SourceForge DTD URL

我找到的另一个解决方案是将DTD URL更改为SourceForge并将声明从PUBLIC更改为SYSTEM.

如果您的系统处于脱机状态,下面也可以使用.

<!DOCTYPE hibernate-configuration SYSTEM 
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
Run Code Online (Sandbox Code Playgroud)

Hibernate脱机工作


小智 5

  • 另一种方法是你应该下载DTD文件并设置文件路径.并在java Build path(eclipse)中设置dtd文件位置.

Hibernate的配置

Orignal DTD

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
Run Code Online (Sandbox Code Playgroud)

修正了DTD

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://localhost:8080/YourProject/DTDLocation/hibernate-configuration-3.0.dtd">
Run Code Online (Sandbox Code Playgroud)

Hibernate的映射

Orignal DTD

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
Run Code Online (Sandbox Code Playgroud)

修正了DTD

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://localhost:8080/YourProject/DTDLocation/hibernate-mapping-3.0.dtd">
Run Code Online (Sandbox Code Playgroud)