Java 离线时无法解析 hibernate.cfg.xml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4301294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't parse hibernate.cfg.xml while offline
提问by ripper234
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 onlyhappens when I'm offline. Does hibernate try to read the DTD when parsing the config? What's the root cause here?
这仅在我离线时发生。解析配置时,休眠是否尝试读取 DTD?这里的根本原因是什么?
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>
采纳答案by JRS
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. I had multiple versions of hibernate3.jar on the classpath causing an old incompatible version of the DTD Entity Resolverto be loaded which only works with the old namespace (http://hibernate.sourceforge.net/). For reference, here's a link to the newer DTD Entity Resolver.
升级到 Hibernate 3.6.8.Final 后,我遇到了同样的问题。我在类路径上有多个版本的 hibernate3.jar,导致加载旧的不兼容版本的DTD 实体解析器,该版本仅适用于旧的命名空间 ( http://hibernate.sourceforge.net/)。作为参考,这里有一个指向更新的DTD Entity Resolver的链接。
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>
回答by iruediger
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
回答by iruediger
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.
希望它能解决你的问题。
回答by u445908
I think you are using Hibernate3 jar file, but the Hibernate4 DTD file. so the solution is choose one of them: 3 or 4.
我认为您使用的是 Hibernate3 jar 文件,但使用的是 Hibernate4 DTD 文件。所以解决方案是选择其中之一:3 或 4。
BTW, I strongly recommend you using Maven for the jar dependency management.
顺便说一句,我强烈建议您使用 Maven 进行 jar 依赖项管理。
回答by 452
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>
回答by atom217
In your mapping files, you must have the exactly SAME doctype as found in the mapping DTD's.
在您的映射文件中,您必须具有与在映射 DTD 中找到的完全相同的文档类型。
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. No local dtd's in your projects to solve this issue, no code to intercept. :-)
只有这样你才会看到hibernate3.jar中的dtd可以通过classpath找到,在防火墙后面运行,单机等等都没有问题。您的项目中没有本地 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.
这当然也适用于配置文件。
回答by vegemite4me
In case this helps anyone else ... my problem was that I was including the wrong Maven artifact. I was including spring-hibernate3
:
如果这对其他人有帮助……我的问题是我包含了错误的 Maven 工件。我包括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>
回答by danielcode7
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.
因此,www
必须在配置文件和所有映射文件中声明 URI 中的 。
It is not your case (cause I can see you have the http://www...
URI), but it may help somebody.
这不是你的情况(因为我可以看到你有http://www...
URI),但它可能对某人有所帮助。
Regards.
问候。
回答by Stephan Struckmann
Alberto Daniel actually is right, indeed adding the "www." fixed for me the problem.
Alberto Daniel 实际上是对的,确实添加了“www”。为我解决了这个问题。
I guess, since the hibernate-core.jar file contains the dtd files, exactly the location http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtdis treated somehow specially by the XML parser, so that the dtd from the jar file is used. I did not verify this, but it may be an explanation. However: Thank you Alberto!
我想,由于 hibernate-core.jar 文件包含 dtd 文件,因此 XML 解析器以某种方式特别处理了http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd的位置,以便使用 jar 文件中的 dtd。我没有验证这一点,但它可能是一个解释。但是:谢谢阿尔贝托!
Regards.
问候。
回答by Anirudh Jadhav
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 */