无法解析配置:hibernate.cfg.xml。根元素类型的文档类型声明" hibernate-configuration"必须以'>'结尾

时间:2017-07-10 15:37:54

标签: xml hibernate

我知道这个问题已被多次询问过。我尝试了所有这些但是所有的努力都是徒劳的。

这是我的hibernate.cfg.xml文件

`

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

<hibernate-configuration>  

    <session-factory>  
       <property name="show_sql">true</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 
<property name="connection.url">XXX</property> 
<property name="username">XXX</property> 
<property name="password">XXX</property> 
<property name="connection.pool_size">1</property>  
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> 
<property name="hbm2ddl.auto">update</property>
<mapping resource="user.hbm.xml"/>
    </session-factory>  

</hibernate-configuration>

`

每当我将 DOCTYPE 更改为 PUBLIC 时,我都会收到以下错误。

`

Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1491)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
    at com.javatpoint.mypackage.StoreData.main(StoreData.java:13)
Caused by: org.dom4j.DocumentException: hibernate.sourceforge.net Nested exception: hibernate.sourceforge.net
    at org.dom4j.io.SAXReader.read(SAXReader.java:484)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481)
    ... 2 more

`

当我将其更改为 SYSTEM 时,我会收到以下错误。

`

Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1491)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
    at hibernate.TableView.main(TableView.java:15)
Caused by: org.dom4j.DocumentException: Error on line 4 of document  : The document type declaration for root element type "hibernate-configuration" must end with '>'. Nested exception: The document type declaration for root element type "hibernate-configuration" must end with '>'.
    at org.dom4j.io.SAXReader.read(SAXReader.java:482)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481)
    ... 2 more

`

user.hbm.xml文件

`

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping SYSTEM 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  

 <hibernate-mapping>  
  <class name="hibernate.User1" table="User1">  
    <id name="id" type="integer">  
    <column name="P_ID" precision="4" scale="0" />
     <generator class="assigned"></generator>  
    </id>  

  <property name="name" type="string">
            <column name="NAME" length="30" not-null="true" />
        </property> 

       <property name="EMAIL_ID" type="string">
            <column name="EMAIL" length="40" not-null="true" />
        </property>
       <property name="COUNTRY" type="string">
            <column name="COUNTRY" length="30" not-null="true" />
        </property> 
        <property name="PASSWORD" type="string">
            <column name="PASSWORD" length="30" not-null="true" />
        </property>    
  </class>  

 </hibernate-mapping>  

` enter image description here

enter image description here

这是我的第一个hibernate项目。你能告诉我哪里出错了。我在连接到互联网的同时运行这些程序。

1 个答案:

答案 0 :(得分:0)

我通过为映射和配置下载DTD文件并在hibernate.hbm.xml和user.hbm.xml文件中引用其路径来解决我的问题。

hibernate.hbm.xml

`

123,45KB  
123,45MB  
1,23GB  

`

user.hbm.xml

`

<!DOCTYPE hibernate-configuration PUBLIC 
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "C:/XXX/Test/hibernate/lib/hibernate-configuration-3.0.dtd">

`

由于