Are you using JPA and getting the error "Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named ..."
You might have used an entity manager as the following code:
And you might have a persistence.xml file as below:
Now see that, this error may happen for any of the following reasons:
1. In the code EntityManagerFactory emf = Persistence.createEntityManagerFactory("TestPU"); TestPU is passed in the method which must be a valid persistence unit name. In the persistence.xml file the name is defined as persistence-unit name="PersonPU". These two names must be same. So to remove the error the correct code will be :
2. If the persistence unit name is correctly used but still the error exists, then confirm that the required jar file containing the JPA implementation classes are available in the classpath. Required jar files for the above code are eclipselink-2.0.0.jar and eclipselink-javax.persistence-2.0.jar
You might have used an entity manager as the following code:
public void persist(Object object) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("TestPU");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
try {
em.persist(object);
em.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
em.getTransaction().rollback();
} finally {
em.close();
}
}
And you might have a persistence.xml file as below:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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">
<persistence-unit name="PersonPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>person.entity.Person</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/persondb"/>
<property name="javax.persistence.jdbc.password" value="mypassword"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="root"/>
</properties>
</persistence-unit>
</persistence>
Now see that, this error may happen for any of the following reasons:
1. In the code EntityManagerFactory emf = Persistence.createEntityManagerFactory("TestPU"); TestPU is passed in the method which must be a valid persistence unit name. In the persistence.xml file the name is defined as persistence-unit name="PersonPU". These two names must be same. So to remove the error the correct code will be :
EntityManagerFactory emf = Persistence.createEntityManagerFactory("PersonPU");
2. If the persistence unit name is correctly used but still the error exists, then confirm that the required jar file containing the JPA implementation classes are available in the classpath. Required jar files for the above code are eclipselink-2.0.0.jar and eclipselink-javax.persistence-2.0.jar
0 comments:
Post a Comment