java - Hibernate:无法解析配置:hibernate.cfg.xml?

标签 java xml hibernate configuration

尝试在我的应用程序中使用 Hibernate 时出现以下错误:

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

这是什么原因造成的?请参阅下面的 hibernate.cfg.xmloutputRunner 类

注意:我看过this answer并尝试使用建议来修复它,但没有成功。

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>

输出:

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

运行类别:

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();

    }
}

编辑:将鼠标悬停在 session 工厂标记上时出现错误:

enter image description here

最佳答案

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

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

关于java - Hibernate:无法解析配置:hibernate.cfg.xml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34301825/

相关文章:

具有数千个连接和分布式事务的 PostgreSQL DB 性能问题

java - 没有数据库的 Hibernate/JPA

java - 谷歌端点

java - 如何使用 Tomcat 6 配置 Struts 2?

c# - 具有多个命名空间的 XML 反序列化

c# - Xml 反序列化在空元素上失败

java - 如何使用 JDBC 在 spring-session 中初始化模式

java - 如何从apache的常见数学DescriptiveStatistics<Double>创建List<Double>?

java - 是什么导致 "Unable to retrieve native address from ByteBuffer object"?

java - 编译时找不到符号错误。