I'm a beginner with JPA and encounter some difficulties I don't have any idea how to solve them. While reading many forums, I see that a lot of people also have the same.
I work with : Eclipse Mars / H2 database / GlassFish 4 / Maven The project I created is a : Simple JPA project
The error I got is :
Exception in thread "main" javax.persistence.PersistenceException: Invalid persistence.xml. Error parsing XML (line-1 : column -1): cvc-elt.1 : Déclaration de l'élément 'persistence' introuvable.
It is said that the declaration of the persistence object is unfindable.
But my Persistence.xml exists and is set in the META-INF root.
<?xml version="1.0" encoding="UTF-8"?> <persistence 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" version="2.1"> <persistence-unit name="CITIZEN2P0" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <class>com.citizen.entities.User</class> <class>com.citizen.entities.Groupe</class> <class>com.citizen.entities.Organisation</class> <class>com.citizen.entities.Message</class> <class>com.citizen.entities.RoleUserInGroup</class> <class>com.citizen.entities.Courrier</class> <properties> <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/> <property name="javax.persistence.jdbc.url" value="jdbc:h2:~/test"/> <property name="javax.persistence.jdbc.user" value="sa"/> <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/> </properties> </persistence-unit> </persistence>
In order to use the persistence.xml, I have a class with an Entity Manager Factory :
package com.citizen.run; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; import com.citizen.entities.User; public class Test { public static void main(String[]argv) { EntityManagerFactory emf = Persistence.createEntityManagerFactory("CITIZEN2P0"); EntityManager em = emf.createEntityManager(); EntityTransaction transac = em.getTransaction(); transac.begin(); User user = new User(); user.setNomUser("nom4"); user.setPrenomUser("prenom4"); em.persist(user); transac.commit(); em.close(); emf.close(); } }
I really don't know what to do !!
Thanx for any help.
No comments:
Post a Comment