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

时间:2013-12-28 19:56:24

标签: java hibernate jpa persistence

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

  

引起: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>

3 个答案:

答案 0 :(得分:1)

提供的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 :(得分:1)

我曾经遇到过类似的问题(过去找不到元素和实体映射的声明)当我使用JPA版本2.0&amp;的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>

答案 2 :(得分:-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>