每当我断开互联网连接时,都会收到以下异常:
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.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>