hibernate之javax.persistence.PersistenceException之JPA+ hibernate
我是 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.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>
我在这上面花了一大堆时间。我希望我花的时间可以节省你的时间!
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。