Can not find the declaration of element 'persistence'
Asked Answered
P

5

23

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:

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'

<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>
Peugia answered 28/12, 2013 at 19:56 Comment(2)
Please format the XML correctly. Get rid of all those >.Assemblage
I ran to this same issue few weeks ago. Are you creating entityManagerFactory manually? It looks like you are passing "persistence.xml" to constructor, which can't open/find this file. For me, everything worked fine when entity manager was created by container.Vinculum
P
-6

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>    

Peugia answered 30/12, 2013 at 11:50 Comment(1)
"I do not know exactly what was wrong" - in my answer I told you exactly what was wrong. Pick the correct answer: meta.stackoverflow.com/help/someone-answersEgoist
N
48

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

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

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

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

Nealey answered 14/1, 2014 at 14:1 Comment(0)
R
6

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.

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> 
Roughrider answered 28/12, 2013 at 20:16 Comment(1)
The JPA is version 2.1. When I put the tag xml version 1.0 of the errorPeugia
T
2

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.

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.

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>
Therefrom answered 10/11, 2015 at 19:1 Comment(0)
T
1

If you have copy pasted code from older versions then you might be importing the annotation using javax but it has been changed to jakarta. You would have missed this dependency- Spring data JPA for Maven projects.

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

add this one in your POM under the dependencies section and then use ctrl+click on @Entity to import it from.

Earlier it was like this:

import javax.persistence.Entity;

and then changed to:

import jakarta.persistence.Entity;
Turnedon answered 12/8, 2023 at 0:9 Comment(0)
P
-6

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>    

Peugia answered 30/12, 2013 at 11:50 Comment(1)
"I do not know exactly what was wrong" - in my answer I told you exactly what was wrong. Pick the correct answer: meta.stackoverflow.com/help/someone-answersEgoist

© 2022 - 2024 — McMap. All rights reserved.