gpt4 book ai didi

hibernate - javax.persistence.PersistenceException - JPA+ hibernate

转载 作者:行者123 更新时间:2023-12-01 08:07:51 27 4
gpt4 key购买 nike

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

我无法修复这个错误,你能帮我解决这个问题吗?

干杯

拉杰什

持久化.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>

应用程序.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();

}

}

错误:

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)

最佳答案

问题是您的 与您的 hibernate 库要求不匹配。从可靠样本中复制并粘贴带有命名空间 (xmlns) 的整个持久性标记。然后在下方检查您的库版本。

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

JPA 版本与持久化版本相同。 ORM 是hibernate 的版本。所以你的持久性文件有 <persistence version="2.1" ... > .所以,到目前为止,您只需要下面的 hibernate 库和版本(或更高版本):

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

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

我在这上面花了一大堆时间。我希望我花的时间可以节省你的时间!

关于hibernate - javax.persistence.PersistenceException - JPA+ hibernate ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20176545/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com