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)