找不到元素 '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>

解决方法

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

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

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

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...