找不到元素 'persistence' 的声明,缺少元素持久性

时间:2021-02-15 17:46:44

标签: java hibernate jpa persistence

已经把persistence.xml放在eclipse中项目的classpath中,因为之前的错误是找不到文件。现在给出这个错误:

Caused by: javax.persistence.PersistenceException: Invalid persistence.xml. Error parsing XML [line : -1, column : -1] : cvc-elt.1: Can not find the declaration of element 'persistence'

enter image description here

这是我的文件:

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    version="2.1"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="DataSource" transaction-type="JTA">
        <description>JTA persistence unit related to the datasource DataSource</description>
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.ejb.cfgfile" value="hibernate_DataSource.cfg.xml"/>
        </properties>
    </persistence-unit>

    <persistence-unit name="securityStore" transaction-type="JTA">
        <description>JTA persistence unit related to the datasource securityStore</description>
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
           <property name="hibernate.ejb.cfgfile" value="hibernate_securityStore.cfg.xml"/>
        </properties>
    </persistence-unit>
</persistence>

1 个答案:

答案 0 :(得分:0)

根据 JPA 规范(请参阅第 8.2.1 节 persistence.xml 文件):

<块引用>

persistence.xml 文件定义了一个持久性单元。 persistence.xml 文件位于持久性单元根目录的 META-INF 目录中。

因此,尝试将您的 persistence.xml 放在 META-INF 目录中。

附言也许您的应用程序实际上使用的不是您期望的 persistence.xml,而是放置在 META-INF 目录中的类路径中的某个位置。