java 休眠:无法解析配置:hibernate.cfg.xml?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34301825/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:43:22  来源:igfitidea点击:

Hibernate: Could not parse configuration: hibernate.cfg.xml?

javaxmlhibernateconfiguration

提问by java123999

I am getting the following error when trying to use Hibernatewithing my application:

尝试与Hibernate我的应用程序一起使用时出现以下错误:

org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml

What is causing this? Please see hibernate.cfg.xml, outputand Runner classbelow?

这是什么原因造成的?请看hibernate.cfg.xmloutputRunner class下面?

Note: I have looked at this answerand tried to fix it using suggestions, but with no luck.

注意:我查看了这个答案并尝试使用建议修复它,但没有运气。

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:3306/test-db</property>
            <property name="connection.username">user</property>
            <property name="connection.password">password</property>

            <!-- SQL dialect -->
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

            <!-- Echo all executed SQL to stdout -->
            <property name="show_sql">true</property>

            <!-- Use XML-based mapping metadata --> 
            <!-- <mapping resource="domain/Message.hbm.xml"/> -->

            <!-- Use Annotation-based mapping metadata -->
            <mapping class="entity.Message"/>            

        </session-factory>
    </hibernate-configuration>

Output:

输出:

Successfully started process 'command 'C:\Program Files\Java\jdk1.8.0_65\bin\java.exe''
Dec 16, 2015 12:01:20 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
Dec 16, 2015 12:01:20 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.5.Final}
Dec 16, 2015 12:01:21 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Dec 16, 2015 12:01:21 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Dec 16, 2015 12:01:21 AM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: hibernate.cfg.xml
Dec 16, 2015 12:01:21 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: hibernate.cfg.xml
Initial SessionFactory creation failed.org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
    at util.HibernateUtil.buildSessionFactory(HibernateUtil.java:25)
    at util.HibernateUtil.<clinit>(HibernateUtil.java:12)
    at client.HelloWorldClient.main(HelloWorldClient.java:13)
Caused by: org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2163)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2075)
    at util.HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
    ... 2 more
Caused by: org.dom4j.DocumentException: Error on line 2 of document  : The processing instruction target matching "[xX][mM][lL]" is not allowed. Nested exception: The processing instruction target matching "[xX][mM][lL]" is not allowed.
    at org.dom4j.io.SAXReader.read(SAXReader.java:482)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2155)
    ... 4 more
:run FAILED
:run (Thread[Daemon worker Thread 15,5,main]) completed. Took 0.617 secs.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':run'.
> Process 'command 'C:\Program Files\Java\jdk1.8.0_65\bin\java.exe'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --debug option to get more log output.

BUILD FAILED

Runner Class:

跑者类:

package client;

import org.hibernate.Session;

import util.HibernateUtil;
import entity.Message;


public class HelloWorldClient {
    public static void main(String[] args) {

                Session session = HibernateUtil.getSessionFactory().openSession();
                session.beginTransaction();

                Message message = new Message( "Hello World with Hibernate & JPA Annotations" ); 

                session.save(message);

                session.getTransaction().commit();
                session.close();

    }
}

Edit: Error seen when hovering over session-factory tag :

编辑:将鼠标悬停在会话工厂标签上时出现错误:

enter image description here

在此处输入图片说明

回答by Vivek

This is because your internet is blocking to get data from that DTD. Just dowbload all DTD which is mention in xml file and in all xml file give location like this.. This works for me

这是因为您的互联网阻止从该 DTD 获取数据。只需下载 xml 文件中提到的所有 DTD,并在所有 xml 文件中给出这样的位置..这对我有用

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "C:\Downloads\hibernate-mapping-3.0.dtd"> <hibernate-configuration>

回答by Prakhar Agrawal

I was facing the same problem, so i just downloaded that file mentioned in the configuration file and placed it to the path at '/home/ist/Downloads/hibernate-configuration-3.0.dtd' and gave that path to the configuration file and it worked. Hope its helpful for you too.

我遇到了同样的问题,所以我只是下载了配置文件中提到的那个文件,并将它放到了“/home/ist/Downloads/hibernate-configuration-3.0.dtd”的路径中,并将该路径提供给了配置文件和有效。希望它对你也有帮助。

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "/home/ist/Downloads/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- JDBC Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/HibernateDB</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>

        <!-- JDBC connection pool settings ... using built-in test pool -->
        <property name="connection.pool_size">10</property>

        <!-- Select our SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Echo the SQL to stdout -->
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>


        <property name="hbm2ddl.auto">create
        </property>

        <mapping resource="resource/actor.hbm.xml"/>



    </session-factory>

</hibernate-configuration>

回答by user987339

In order to be parsed as xml, your hibernate.cfg.xml should start with:

为了被解析为 xml,你的 hibernate.cfg.xml 应该以:

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

Check following things in hibernate.cfg.xml:

检查 hibernate.cfg.xml 中的以下内容:

  1. check if there are some blank space or other content before the <?xml ?>
  2. check if there is another <?xml ?>definition in hibernate.cfg.xml
  3. check if there is a Byte Order Mark (BOM) before the <?xml ?>(http://www.w3.org/International/questions/qa-byte-order-mark#remove)
  1. 检查之前是否有一些空格或其他内容 <?xml ?>
  2. 检查<?xml ?>hibernate.cfg.xml 中是否还有其他定义
  3. 检查<?xml ?>http://www.w3.org/International/questions/qa-byte-order-mark#remove)之前是否有字节顺序标记(BOM )

回答by Nallamachu

Could you please try with the below configuration details and make sure you have placed the hibernate.cfg.xmlfile under the SRC directory. If you have to placed cfg file other than SRC, you have to specify the path in Configuration() at the time of creating SessionFactory.

您能否尝试使用以下配置详细信息,并确保您已将hibernate.cfg.xml文件放在 SRC 目录下。如果必须放置SRC以外的cfg文件,则必须在创建SessionFactory时在Configuration()中指定路径。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property>
        <property name="hibernate.connection.url"> jdbc:mysql://localhost:3306/hibernatex </property>
        <property name="hibernate.connection.username"> root </property>
        <property name="hibernate.connection.password"> root </property>
        <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property>
        <mapping resource="com/ora/hibernate/examples/Employee.hbm.xml"
          package="com.ora.hibernate.examples" />
     </session-factory>
</hibernate-configuration>

回答by java123999

I was able to solve what the issue was. The issue was that my hibernate.cfg.xmlfile was not under the "resources"directory within my project.

我能够解决问题所在。问题是我的hibernate.cfg.xml文件不在我的项目中的“资源”目录下。

This solved all the issues I was having, and I did notneed to alter the code within the file.

这解决了所有我遇到的问题,我也没有需要改变的文件中的代码。

回答by tharindu_DG

Something wrong in the second line of the config file. Try changing the location of the DTD file as follows.

配置文件的第二行有问题。尝试如下更改 DTD 文件的位置。

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

More Info

更多信息

Hope this helps.

希望这可以帮助。

回答by Rahul Sirwani

Many Times it Happens because of Internet not Connected on your System.To Work on Hibernate Internet Is Compulsary.

很多时候它发生是因为您的系统上没有连接互联网。在休眠互联网上工作是强制性的。