Java 找不到元素“持久性”的声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20818737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:34:19  来源:igfitidea点击:

Can not find the declaration of element 'persistence'

javahibernatejpapersistence

提问by csf

Have put the persistence.xml in the classpath of the project in eclipse because before the error was that the file was not found. Now gives this error:

在eclipse中把persistence.xml放在项目的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'

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

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

采纳答案by csf

Solved!

解决了!

I do not know exactly what was wrong, but it worked well:

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

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

回答by Oliver Shaw

There is something slightly wrong with the XML provided, perhaps a missing version, perhaps the XML definition. Could also be a strange character or a typo somewhere.

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

A working template is below, try that instead.

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

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

回答by Marcel St?r

The problem is that you mix JPA 2.0 and JPA 2.1 notation.

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

Either this

要么这

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

for JPA 2.1 or this

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

for JPA 2 but not a mix thereof.

适用于 JPA 2 但不是它们的混合。

See http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/persistence/index.htmlfor details.

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

回答by Arun B Chandrasekaran

I have faced similar problem (Cannot find the declaration of element 'entity-mappings') in the past when I had persistence.xml with JPA version 2.0 & orm.xml file with version 2.1. I think the error reported above are similar.

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

Working samples for JPA 2. Read the sample below carefully and note their version. Ensure they are of same versions as in the samples. You may use JPA 2.1 and approprite schema reference as well.

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

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>