简体   繁体   English

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

[英]Can't parse hibernate.cfg.xml while offline

Whenever I'm disconnected from the internet, I get the following exception: 每当我断开互联网连接时,都会收到以下异常:

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

This only happens when I'm offline. 在我离线时发生。 Does hibernate try to read the DTD when parsing the config? 休眠时,在解析配置时是否尝试读取DTD? What's the root cause here? 根本原因是什么?

Here is my hibernate.cfg.xml: 这是我的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 can resolve the DTDs locally (without a network connection). Hibernate可以在本地解析DTD(无需网络连接)。

Your DOCTYPE is using the new namespace ( http://www.hibernate.org/dtd/ ) for Hibernate 3.6, so you might have an older version of the Hibernate libraries in your classpath. 您的DOCTYPE使用的是Hibernate 3.6的新名称空间( http://www.hibernate.org/dtd/ ),因此您的类路径中可能具有较旧版本的Hibernate库。

I experienced the same issue after upgrading to Hibernate 3.6.8.Final. 升级到Hibernate 3.6.8.Final后,我遇到了相同的问题。 I had multiple versions of hibernate3.jar on the classpath causing an old incompatible version of the DTD Entity Resolver to be loaded which only works with the old namespace ( http://hibernate.sourceforge.net/ ). 我在类路径上有hibernate3.jar的多个版本,导致加载了旧的不兼容版本的DTD实体解析器 ,该版本仅适用于旧的命名空间( http://hibernate.sourceforge.net/ )。 For reference, here's a link to the newer DTD Entity Resolver . 作为参考,这是更新的DTD实体解析器的链接。

I'm using hibernate3-maven-plugin which has a transitive dependency on an older version of Hibernate so I just had to specify a plugin dependency on Hibernate 3.6.8.Final. 我正在使用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>

It is not possible because, hibernate jar files is also load the some dtd content but slow internet connection it is worked. 这是不可能的,因为休眠的jar文件也加载了某些dtd内容,但是互联网连接速度很慢。

(1) Hibernate Configuration File Location (1)休眠配置文件位置

The first solution was to provide the DTD file location in the system using classpath. 第一个解决方案是使用类路径在系统中提供DTD文件位置。 So the DocType that worked offline would be; 因此,可以脱机工作的DocType应该是:

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

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

Another solution I found working is when I change the DTD URL to SourceForge and changed the declaration from PUBLIC to SYSTEM. 我发现工作的另一个解决方案是将DTD URL更改为SourceForge,并将声明从PUBLIC更改为SYSTEM。

So below will also work if your system is offline. 因此,如果您的系统处于离线状态,则下面的内容同样适用。

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

Hibernate Work Offline 休眠脱机工作

  • Another Way is You should download DTD file and set the file path. 另一种方法是您应该下载DTD文件并设置文件路径。 and also set the dtd file location in java Build path (eclipse). 并在Java构建路径(eclipse)中设置dtd文件的位置。

Hibernate-configuration 休眠配置

Orignal DTD 原始DTD

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

Corrected DTD 更正的DTD

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://localhost:8080/YourProject/DTDLocation/hibernate-configuration-3.0.dtd">

Hibernate-mapping 休眠映射

Orignal DTD 原始DTD

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

Corrected DTD 更正的DTD

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://localhost:8080/YourProject/DTDLocation/hibernate-mapping-3.0.dtd">

in my situation: JBoss AS 7 在我的情况下:JBoss AS 7

I check: 我检查:

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

and exclude dom4j in pom.xml 并在pom.xml中排除dom4j

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.1.9-SNAPSHOT</version>
        <exclusions>
            ...........
            <exclusion>
                <artifactId>dom4j</artifactId>
                <groupId>dom4j</groupId>
            </exclusion>
        </exclusions>
    </dependency>

In case this helps anyone else ... my problem was that I was including the wrong Maven artifact. 万一这对其他人有帮助...我的问题是我包括了错误的Maven工件。 I was including spring-hibernate3 : 我当时包括spring-hibernate3

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-hibernate3</artifactId>
        <version>2.0.8</version>
    </dependency>

Replacing it with spring-orm fixed this issue: spring-orm替换它可以解决此问题:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>2.5.6.SEC03</version>
    </dependency>

I think you are using Hibernate3 jar file, but the Hibernate4 DTD file. 我认为您使用的是Hibernate3 jar文件,但使用的是Hibernate4 DTD文件。 so the solution is choose one of them: 3 or 4. 所以解决方案是选择以下三个之一:

BTW, I strongly recommend you using Maven for the jar dependency management. 顺便说一句,我强烈建议您使用Maven进行jar依赖管理。

Just check this site https://forum.hibernate.org/viewtopic.php?f=1&t=943281&start=0 只需检查此站点https://forum.hibernate.org/viewtopic.php?f=1&t=943281&start=0

Hope that it will solve your problem. 希望它能解决您的问题。

In your mapping files, you must have the exactly SAME doctype as found in the mapping DTD's. 在映射文件中,您必须具有与映射DTD完全相同的SAME文档类型。

Then and only then you'll see that the dtd's found in hibernate3.jar can be found through the classpath, and running behind a firewall, stand-alone, etc. will be no problem at all. 然后,只有到那时,您才会看到可以通过类路径找到hibernate3.jar中找到的dtd,并且在防火墙,独立等等之后运行完全没有问题。 No local dtd's in your projects to solve this issue, no code to intercept. 您的项目中没有本地dtd可以解决此问题,没有代码可拦截。 :-) :-)

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

The same is of course applicable for the configuration file. 当然,这同样适用于配置文件。

I have used following method to skip the validation of Doctype in the Configuration file 我已使用以下方法跳过配置文件中Doctype的验证

Code:- 码:-

public static Document parseConfiguration(String resourcePath) throws Exception {

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();



        factory.setValidating(false); 

        DocumentBuilder builder = factory.newDocumentBuilder();

         File f=new File(resourcePath);


        FileInputStream fs=new FileInputStream(f);

        Document dtd = builder.parse(fs);

        return dtd;

        } 



public static void main(String[] args)
 { 

   Document dtd=null;

        try {
            dtd = parseConfiguration("src/hibernate.cfg.xml");

        } catch (Exception e) {

            e.printStackTrace();
        }

        SessionFactory  factory=new AnnotationConfiguration()  
         .configure(dtd).buildSessionFactory(); 
/*
Now this code worked for me 
You just have to use annotation instead of hbm.xml file because I was not able to skip the validation of mapping file as it is written inside the cfg.xml file
Reply if you got some other answer to run hibernate application in offline */

instead of 代替

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

use 采用

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

it worked fine for me 对我来说很好

You can use an internal DTD (not pretty IMO) or download the DTD file to your filesystem. 您可以使用内部DTD(不是漂亮的IMO)或将DTD文件下载到文件系统。

Check W3Schools' for more information: http://www.w3schools.com/dtd/dtd_intro.asp 检查W3Schools'以获取更多信息: http : //www.w3schools.com/dtd/dtd_intro.asp

I had this problem too. 我也有这个问题。 My DOCTYPE was: 我的DOCTYPE是:

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">

It should be: 它应该是:

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

Can you see the difference? 你能看到区别么? The first URI has NOT www and the second URI has www 第一个URI没有www ,第二个URI有www

So, the www in the URI must be declared in the configuration file and in all the mapping files. 因此,必须在配置文件和所有映射文件中声明URI中的www

It is not your case (cause I can see you have the http://www... URI), but it may help somebody. 这不是您的情况(因为我可以看到您拥有http://www... URI),但它可能对某些人有所帮助。

Regards. 问候。

Alberto Daniel actually is right, indeed adding the "www." Alberto Daniel实际上是正确的,确实添加了“ www”。 fixed for me the problem. 为我解决了这个问题。

I guess, since the hibernate-core.jar file contains the dtd files, exactly the location http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd is treated somehow specially by the XML parser, so that the dtd from the jar file is used. 我猜,由于hibernate-core.jar文件包含dtd文件,因此XML解析器会以某种方式特别对待http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd位置,因此使用jar文件中的dtd。 I did not verify this, but it may be an explanation. 我没有验证这一点,但这可能是一个解释。 However: Thank you Alberto! 但是:谢谢阿尔贝托!

Regards. 问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM