Невозможно найти декларацию элемента 'persistence'
Поместите persistence.xml в путь к классам проекта в eclipse
потому что до ошибки было то, что файл не был найден.
Теперь выдает эту ошибку:
Вызвано: javax.persistence.PersistenceException: недействительным persistence.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>
Ответы
Ответ 1
Решено!
Я точно не знаю, что было не так, но все получилось хорошо:
<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>
Ответ 2
Проблема заключается в том, что вы смешиваете 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.
Ответ 3
Есть что-то немного неправильное с предоставленным 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>
Ответ 4
У меня возникла аналогичная проблема (не удалось найти объявление элемента "сущности-сопоставления" ) в прошлом, когда у меня был persistence.xml с JPA версии 2.0 и orm.xml с версией 2.1. Я думаю, что приведенная выше ошибка аналогична.
Рабочие образцы для 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>