找不到元素'persistence'的声明

问题描述 投票:0回答:5

已将 persistence.xml 放入 eclipse 中项目的类路径中 因为之前的错误是找不到该文件。 现在出现这个错误:

原因:javax.persistence.PersistenceException:无效 持久性.xml。解析 XML 时出错 [行:-1,列:-1]: cvc-elt.1:找不到元素“persistence”的声明

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1"
             xsi:schemalocation="http://java.sun.com/xml/ns/persistence
                                 http://java.sun.com/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/Automoveis" />
            <property name="javax.persistence.jdbc.user" value="postgres" />
            <property name="javax.persistence.jdbc.password" value="1234" />
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.jdbc.Driver" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="create" />
        </properties>
    </persistence-unit>
</persistence>
java hibernate jpa persistence
5个回答
48
投票

问题在于您混合了 JPA 2.0 和 JPA 2.1 表示法。

无论是这个

<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">

对于 JPA 2.1 或这个

<persistence 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"
  version="2.0">

适用于 JPA 2,但不是其混合。

有关详细信息,请参阅 http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/persistence/index.html


5
投票

提供的 XML 有点问题,可能是缺少版本,也可能是 XML 定义。也可能是一个奇怪的字符或某个地方的拼写错误。

下面是一个工作模板,请尝试一下。

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
         version="2.0">
....
</persistence> 

1
投票

过去,当我有 JPA 版本 2.0 的 persistence.xml 和版本 2.1 的 orm.xml 文件时,我遇到过类似的问题(找不到元素“实体映射”的声明)。我认为上面报告的错误是类似的。

JPA 2 的工作示例。仔细阅读下面的示例并记下其版本。确保它们与示例中的版本相同。您也可以使用 JPA 2.1 和适当的架构参考。

persistence.xml

<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">
    <persistence-unit name="SANJUSEJB" transaction-type="JTA">
        <jta-data-source>jdbc/sanjusDataSourceXA</jta-data-source>
        <mapping-file>META-INF/orm.xml</mapping-file>
        <class>org.sanjus.pa.ejb.entity.UserEntity</class>
    </persistence-unit>
</persistence>

orm.xml

<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd" version="2.0">
    <named-query name="findUserJSONById">
        <query>SELECT a.userJson FROM UserEntity a WHERE a.userId = :userId</query>
    </named-query>
</entity-mappings>

0
投票

如果您从旧版本复制粘贴代码,那么您可能会使用 javax 导入注释,但它已更改为 jakarta。您可能会错过这个依赖项 - 用于 Maven 项目的 Spring data JPA。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

将此添加到 POM 的依赖项部分下,然后使用 ctrl+click @Entity 导入它。

之前是这样的:

import javax.persistence.Entity;

然后改为:

import jakarta.persistence.Entity;

-6
投票

解决了!

我不知道到底出了什么问题,但效果很好:

<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">    

<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">    
    <provider>org.hibernate.ejb.HibernatePersistence</provider>    

    <properties>    
        <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />    
        <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/Automoveis" />    
        <property name="javax.persistence.jdbc.user" value="postgres" />    
        <property name="javax.persistence.jdbc.password" value="1234" />    
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />               
        <property name="hibernate.hbm2ddl.auto" value="update" />    
        <property name="hibernate.show_sql" value="true" />    
        <property name="hibernate.format_sql" value="true"/>    
    </properties>    
</persistence-unit>    

© www.soinside.com 2019 - 2024. All rights reserved.