我是JPA的新手,当我尝试运行以下代码时,它显示错误为“ cvc-elt.1:无法找到元素声明'持久性'。”
我无法修复此错误,请帮助我解决此问题。
干杯
拉杰什
<?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>
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)
答案 0 :(得分:10)
问题是您的&lt; persistence version =“2.1”...&gt;之间的不匹配和你的hibernate库要求。使用可靠样本中的名称空间(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+
请注意,较新的ORM版本向后兼容较旧的JPA 版本(例如:带有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:
<!-- 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 :(得分:4)
我怀疑您的环境仅支持持久性2.0。尝试更改:
<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 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">
答案 2 :(得分:0)
你错过了xml中的<persistence>
元素...检查你发布的xsd并使你的persistence.xml符合架构。有关简单示例,请参阅this link