文档根元素"持久性",必须匹配DOCTYPE root" null"。 for persistence.xml JPA

时间:2016-06-03 18:39:20

标签: xml hibernate jpa xsd dtd

我收到以下错误

  

文档根元素"持久性",必须匹配DOCTYPE root" null"

以下是导致它的XML文件。

 <?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
        http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd" version="2.1">

        <persistence-unit name="my-persistence">
            <description>Persistence Unit</description>
            <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
            <mapping-file>WEB-INF/config/application-context.xml</mapping-file>
            <!--<jar-file>packedEntity.jar</jar-file>-->
            <class>domain.Applicant</class>
            <class>testScrEntry</class>
            <properties>
                <property name="javax.persistence.jdbc.url" value="oracle.jdbc.OracleDriver"/>
                <property name="javax.persistence.jdbc.user" value="user"/>
                <property name="javax.persistence.jdbc.password" value="password"/>
            </properties>
        </persistence-unit>

    </persistence>

堆栈跟踪

 o.s.w.c.s.XmlWebApplicationContext WARN: Exception encountered during 
 context initialization - cancelling refresh attempt: 
 org.springframework.beans.factory.BeanDefinitionStoreException: Failed 
 to parse configuration class [configuration.JpaConfiguration]; nested 
 exception is java.util.InvalidPropertiesFormatException:  
 org.xml.sax.SAXParseException: Document root element "persistence", must     
 match DOCTYPE root "`null"

当然我用谷歌搜索了错误,它告诉我添加我做了什么,它看起来像:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE persistence PUBLIC xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
 http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">

    <persistence-unit name="my-pu">
        <description>Persistence Unit</description>
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <!--<mapping-file>application-context.xml</mapping-file>-->
        <!--<jar-file>packedEntity.jar</jar-file>-->
        <class>domain.Applicant</class>
        <class>applicant.testScrEntry</class>
        <properties>
            <property name="javax.persistence.jdbc.url" value="oracle.jdbc.OracleDriver"/>
            <property name="javax.persistence.jdbc.user" value="user"/>
            <property name="javax.persistence.jdbc.password" value="password"/>
        </properties>
    </persistence-unit>

</persistence>

但是,上述内容并未说文件必须靠近>。它指的是</persistence>旁边的红色〜。

The public identifier must begin with either a single or double quote character.

---------------------更新1 ------------------------ ------

我试过这个:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE persistence PUBLIC
        "http://xmlns.jcp.org/xml/ns/persistence"
        "http://www.w3.org/2001/XMLSchema-instance"
        "http://xmlns.jcp.org/xml/ns/persistence
        http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd" version="2.1">

错误:org.xml.sax.SAXParseException:The public identifier must begin with either a single or double quote character.

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
             http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">

错误:Document root element "persistence", must match DOCTYPE root "null".

<?xml version="1.0" encoding="UTF-8" ?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

文档根元素&#34;持久性&#34;,必须与DOCTYPE root匹配&#34; null&#34;。

1 个答案:

答案 0 :(得分:1)

你的第二个&#34; XML&#34; document是一个混乱的XSD和DTD结构,最终结果是一个完全不是XML的文档 - 格式不正确,更不用说有效了。

您的第一个XML文档对通过@schemaLocation声明的XSD有效。错误消息是基于DTD的,而不是基于XSD的。将它解析为@schemaLocation中暗示的XSD,你就没事了。