javax.persistence.PersistenceException - JPA + Hibernate [英] javax.persistence.PersistenceException - JPA+Hibernate

查看:137
本文介绍了javax.persistence.PersistenceException - JPA + Hibernate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JPA的新手,当我尝试运行以下代码时,它显示错误为 cvc-elt.1:找不到元素'persistence'的声明。



我无法解决这个错误,请帮我解决这个问题。



干杯

>

Rajesh

Persistance.xml



 <?xml version =1.0encoding =UTF-8?> 
< persistence version =2.1
xmlns =http://xmlns.jcp.org/xml/ns/persistencexmlns: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 >

< persistence-unit name =jpatransaction-type =JTA>

< provider> org.hibernate.ejb.HibernatePersistence< / provider>

< class> com.optirisk.pojo.Department< / class>
< class> com.optirisk.pojo.Employee< / class>

<属性>
< property name =hibernate.show_sqlvalue =true/>
< property name =hibernate.format_sqlvalue =true/>
< property name =hibernate.connection.usernamevalue =NewsLetter1/>
< property name =hibernate.connection.passwordvalue =optiindia2012/>
< property name =hibernate.connection.urlvalue =jdbc:mysql:// localhost:3306 / orpss_hibernate_prototype/>
< property name =hibernate.connection.driver_classvalue =com.mysql.jdbc.Driver/>
< property name =hibernate.dialectvalue =org.hibernate.dialect.MySQLInnoDBDialect/>
< / properties>

< / persistence-unit>
< /余辉>



Application.java



  public class Application {

private static final String PERSISTENCE_UNIT_NAME =jpa;
private static EntityManagerFactory entityManagerFactory;

public static void main(String [] args)throws Exception {

entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);

EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction()。begin();

部门部门=新部门();
department.setDeptName(IT);
entityManager.persist(department);

雇员emp1 =新员工(Peter,ROB,454565);
员工emp2 =新员工(Mathew,Anderson,222);

emp1.setDepartment(department);
emp2.​​setDepartment(department);

entityManager.persist(emp1);
entityManager.persist(emp2);

entityManager.getTransaction()。commit();
entityManager.close();



$ / code>



错误:



 线程main中的异常javax.persistence.PersistenceException:无效的persistence.xml。 
解析XML时出错(line-1:column -1):cvc-elt.1:找不到元素'persistence'的声明。
at org.hibernate.ejb.packaging.PersistenceXmlLoader.loadURL(PersistenceXmlLoader.java:147)
at org.hibernate.ejb.packaging.PersistenceXmlLoader.deploy(PersistenceXmlLoader.java:171)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:325)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:58)
at javax.persistence.Persistence.createEntityManagerFactory (Persistence.java:52)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
at com.jishraa.jpa.Application.main(Application.java:19)


解决方案

问题是您的< persistence version =2.1 ...>和你的休眠库要求。复制并粘贴来自可靠示例的名称空间(xmlns)的整个持久性标记。然后检查下面的库版本。



来自hibernate.org: http://hibernate.org/orm/downloads/


支持的JPA版本

  JPA 1.0:ORM 3.2+ 
JPA 2.0:ORM 3.5+
JPA 2.1:ORM 4.3+
版本(例如:带有JPA 1.0的ORM 4.3)。但是,较新的ORM版本可能
与旧的JPA容器不兼容。


JPA版本与持久性版。 ORM是hibernate的版本。所以你的持久化文件有< persistence version =2.1...> 。因此,截至目前,您只需要hibernate库和以下版本(或更高版本):

  WEB-INF / lib / hibernate-entitymanager-4.3.0.Final.jar 

或Maven pom.xml:

 <! - 对于JPA,使用hibernate-entitymanager而不是hibernate-core  - > 
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-entitymanager< / artifactId>
< version> 4.3.0.Final< / version>
< /依赖关系>

我在这一个上花了大量的时间。我希望我的时间能够拯救你的!


I am new to JPA, when I tried to run the following code, it showing error as "cvc-elt.1: Cannot find the declaration of element 'persistence'."

I cant able to fix this error, could u pls help me out to solve this issue.

Cheers

Rajesh

Persistance.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
    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">

    <persistence-unit name="jpa" transaction-type="JTA">

        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <class>com.optirisk.pojo.Department</class>
        <class>com.optirisk.pojo.Employee</class>

        <properties>
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.connection.username" value="NewsLetter1" />
            <property name="hibernate.connection.password" value="optiindia2012" />
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/orpss_hibernate_prototype" />
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
        </properties>

    </persistence-unit>
</persistence>

Application.java

public class Application {

    private static final String PERSISTENCE_UNIT_NAME = "jpa";
    private static EntityManagerFactory entityManagerFactory;

    public static void main(String[] args) throws Exception {

        entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);

        EntityManager entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();

        Department department = new Department();
        department.setDeptName("IT");
        entityManager.persist(department);

        Employee emp1 = new Employee("Peter", "ROB", "454565");
        Employee emp2 = new Employee("Mathew", "Anderson", "222");

        emp1.setDepartment(department);
        emp2.setDepartment(department);

        entityManager.persist(emp1);
        entityManager.persist(emp2);

        entityManager.getTransaction().commit();
        entityManager.close();

    }

}

Error:

Exception in thread "main" javax.persistence.PersistenceException: Invalid persistence.xml.
Error parsing XML (line-1 : column -1): cvc-elt.1: Cannot find the declaration of element 'persistence'.
    at org.hibernate.ejb.packaging.PersistenceXmlLoader.loadURL(PersistenceXmlLoader.java:147)
    at org.hibernate.ejb.packaging.PersistenceXmlLoader.deploy(PersistenceXmlLoader.java:171)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:325)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:58)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:52)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
    at com.jishraa.jpa.Application.main(Application.java:19)

解决方案

The issue is a mismatch between your <persistence version="2.1"...> and your hibernate library requirements. Copy and paste the whole persistence tag with name spaces (xmlns) from a reliable sample. Then check your library version, below.

From hibernate.org: http://hibernate.org/orm/downloads/

Supported JPA Versions

JPA 1.0: ORM 3.2+
JPA 2.0: ORM 3.5+
JPA 2.1: ORM 4.3+

Note that newer ORM releases are backwards compatible with older JPA versions (ex: ORM 4.3 with JPA 1.0). However, newer ORM releases may not be compatible with older JPA containers.

The JPA version is the same as the persistence version. The ORM is the version of hibernate. So your persistence file has <persistence version="2.1" ... >. So, as of now, you want only the hibernate library and version below (or a later version):

WEB-INF/lib/hibernate-entitymanager-4.3.0.Final.jar

or Maven pom.xml:

<!-- for JPA, use hibernate-entitymanager instead of hibernate-core -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.3.0.Final</version>
</dependency>

I spent a whole bunch of hours on this one. I hope my time spent saves yours!

这篇关于javax.persistence.PersistenceException - JPA + Hibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆