Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';t脱机时解析hibernate.cfg.xml_Java_Hibernate - Fatal编程技术网

Java Can';t脱机时解析hibernate.cfg.xml

Java Can';t脱机时解析hibernate.cfg.xml,java,hibernate,Java,Hibernate,每当我断开与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.AnnotationCon

每当我断开与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>

com.mysql.jdbc.Driver
jdbc:mysql://localhost/foo
用户
通过
1.
org.hibernate.dialogue.mysqlinnodbdialogue
org.hibernate.cache.NoCacheProvider
假的

您可以使用内部DTD(不太好),或者将DTD文件下载到文件系统中

查看W3Schools了解更多信息:

只需查看此网站即可


希望它能解决您的问题。

Hibernate可以在本地解决DTD(无需网络连接)

您的DOCTYPE正在使用Hibernate 3.6的新名称空间(),因此您的类路径中可能有一个旧版本的Hibernate库

升级到Hibernate 3.6.8.Final后,我遇到了同样的问题。我在类路径上有多个版本的hibernate3.jar,导致加载的旧版本不兼容,该版本仅适用于旧名称空间()。为了便于参考,这里有一个到更新版本的链接

我正在使用hibernate3 maven插件,它对旧版本的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>

org.codehaus.mojo
hibernate3 maven插件
2.2
...
org.hibernate
冬眠核心
3.6.8.最终版本

我认为您使用的是Hibernate3 jar文件,而不是Hibernate4 DTD文件。 因此,解决方案是选择其中一个:3或4

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

在我的情况下: JBossAS7

我检查:

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

org.hibernate
冬眠核心
4.1.9-快照
...........
dom4j
dom4j

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

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



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

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

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-hibernate3</artifactId>
        <version>2.0.8</version>
    </dependency>
我也有这个问题。 我的DOCTYPE是:

<!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有
www

因此,URI中的
www
必须在配置文件和所有映射文件中声明

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


问候。

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

我想,因为hibernate-core.jar文件包含dtd文件,所以XML解析器会以某种方式专门处理该位置,以便使用jar文件中的dtd。我没有证实这一点,但这可能是一种解释。不过:谢谢你,阿尔贝托


注意。

我使用以下方法跳过配置文件中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 */

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

(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">

  • 另一种方法是下载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">
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://localhost:8080/YourProject/DTDLocation/hibernate-configuration-3.0.dtd">
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://localhost:8080/YourProject/DTDLocation/hibernate-mapping-3.0.dtd">

校正的DTD

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

休眠映射 原始DTD

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

校正的DTD

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

而不是

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

使用



它对我来说很好

创建了一个问题:向我们展示你的hibernate.cfg。xml@Bozho-这是最基本的。附件。哦,老兄,你太棒了。这帮我节省了很多时间。恢复到“sourceforge”url的类似“”,也会修复它,因为很明显,这些url是在较旧的jar中找到的,请参见Hi Stephen。如果你想知道为什么投反对票,那是因为这本身不是一个答案。当你有更多的代表时,你可以对阿尔伯特·丹尼尔的答案进行评论和投票。在你的问题中解释原因。这会让你在声誉上获得更多分数。要使用相同的doctype来匹配映射DTD的Hanks Haroldo Gondim,而不是DTD的URL,
http://hibernate.org/dtd/hibernate-configuration-3.0.dtd
,在JBoss之外的标准浏览器中解析以下载文件,当JBoss尝试执行相同操作时,我遇到了来自JBoss的HTTP403错误。正在将URL更正为此答案中标识的URL(
http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd
)解决了我的问题。