繁体   English   中英

找不到元素'持久性'的声明

[英]Can not find the declaration of element 'persistence'

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

引发者:javax.persistence.PersistenceException:无效的persistence.xml。 解析XML时出错[line:-1,column:-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>

问题是你混合了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

提供的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> 

我曾经遇到过类似的问题(找不到元素'entity-mappings'的声明),当我使用带有JPA版本2.0的persistence.xml和版本为2.1的orm.xml文件时。 我认为上面报告的错误是相似的。

JPA的工作样本2.仔细阅读下面的示例并记下它们的版本。 确保它们与样品中的版本相同。 您也可以使用JPA 2.1和approprite模式引用。

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>

解决了!

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM