代码之家  ›  专栏  ›  技术社区  ›  ripper234 Jonathan

脱机时无法分析hibernate.cfg.xml

  •  16
  • ripper234 Jonathan  · 技术社区  · 15 年前

    无论何时断开与internet的连接,都会出现以下异常:

    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>
    
    13 回复  |  直到 15 年前
        1
  •  19
  •   JRS    14 年前

    Hibernate可以在本地解析dtd(无需网络连接)。

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

    升级到Hibernate 3.6.8.Final之后,我遇到了同样的问题。我在类路径上有多个hibernate3.jar版本,导致 DTD Entity Resolver 要加载的名称空间仅适用于旧名称空间( http://hibernate.sourceforge.net/ ). 作为参考,这里有一个新的 DTD Entity Resolver .

    我使用的是hibernate3 maven插件,它对Hibernate的旧版本有一个可传递的依赖关系,所以我只需要在hibernate3.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
  •  7
  •   Gautam Viradiya    11 年前

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

    (1)休眠配置文件位置

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

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

    (2)在系统中使用SourceForge DTD URL

    我发现另一个可行的解决方案是将DTD URL更改为SourceForge,并将声明从PUBLIC更改为SYSTEM。

    因此,如果您的系统脱机,下面的命令也将起作用。

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

    Hibernate Work Offline

        3
  •  5
  •   Gautam Viradiya    11 年前
    • 另一种方法是下载DTD文件并设置文件路径。 并在java构建路径(eclipse)中设置dtd文件的位置。

    休眠配置

    原始DTD

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

    修正DTD

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

    休眠映射

    原始DTD

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

    修正DTD

    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://localhost:8080/YourProject/DTDLocation/hibernate-mapping-3.0.dtd">
    
        4
  •  2
  •   452    13 年前

    在我的情况下: JBoss AS 7系列

    我检查:

    <?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>
    

    并在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>
    
        5
  •  2
  •   vegemite4me    13 年前

    如果这能帮助其他人。。。我的问题是我包括了错误的Maven工件。我包括 spring-hibernate3 :

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

    替换为 spring-orm 已修复此问题:

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>2.5.6.SEC03</version>
        </dependency>
    
        6
  •  1
  •   user452425user452425    15 年前

    看看这个网站 https://forum.hibernate.org/viewtopic.php?f=1&t=943281&start=0

    希望它能解决你的问题。

        7
  •  1
  •   Siwei    14 年前

    我认为您使用的是Hibernate3 jar文件,而不是Hibernate4 DTD文件。 所以解决方法是从中选择一个:3或4。

    顺便说一下,我强烈建议您使用Maven进行jar依赖项管理。

        8
  •  0
  •   iruediger    15 年前

    您可以使用内部DTD(不是很好的IMO)或将DTD文件下载到您的文件系统中。

    查看W3Schools'了解更多信息: http://www.w3schools.com/dtd/dtd_intro.asp

        9
  •  0
  •   atom217    13 年前

    在映射文件中,必须具有与在映射DTD中找到的完全相同的doctype。

    然后,只有到那时,您才会看到在hibernate3.jar中找到的dtd可以通过类路径找到,并且在防火墙后面运行、独立运行等都不会有任何问题。 在您的项目中没有本地dtd来解决此问题,没有代码可拦截。:-)

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

    当然,这同样适用于配置文件。

        10
  •  0
  •   László Papp    11 年前

    我使用了以下方法跳过配置文件中Doctype的验证

    代码:-

    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 */
    
        11
  •  0
  •   dur    10 年前

    而不是

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

    使用

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

    对我来说很好

        12
  •  -1
  •   Neniel    11 年前

    我也有这个问题。 我的医生类型是:

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

    应该是:

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

    你能看出区别吗? 第一个URI没有 www 第二个URI有 万维网

    所以 万维网 在URI中必须在配置文件和所有映射文件中声明。

    这不是你的情况(因为我可以看到你有 http://www... 但它可能会帮助某些人。

    当做。

        13
  •  -1
  •   Stephan Struckmann    11 年前

    阿尔贝托丹尼尔实际上是对的,确实添加了“www.”为我解决了这个问题。

    我想,因为hibernate-core.jar文件包含dtd文件,所以 http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd 以某种方式由XML解析器特别处理,以便使用jar文件中的dtd。我没有证实,但这可能是一个解释。不过:谢谢阿尔贝托!

    当做。