Search code examples
javajspjpaeclipselink

Initial SessionFactory creation failed.javax.persistence.PersistenceException


I am doing web application.I found many questions with same problem

Like this question

But I didn't get correct answer. My folder structure and class path everything is correct but why this Exception occurring really I don't know.

Below is my folder structure. Here persistence.xml file is within META-INF folder of src folder

enter image description here

My Persistance.xml:

<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_1_0.xsd"
version="1.0">
  <persistence-unit name="jspWithJpa" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.entity.Student</class>
    <properties>    
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/reports" />
      <property name="javax.persistence.jdbc.user" value="root" />
      <property name="javax.persistence.jdbc.password" value="root" />
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
    </properties>
  </persistence-unit>
</persistence>

EntityManagerUtil

public class EntityManagerUtil {
  private static final EntityManagerFactory emf;
  static {
    try {
      emf = Persistence.createEntityManagerFactory("jspWithJpa");
    } 
    catch (Throwable ex) {
      System.err.println("Initial SessionFactory creation failed." + ex);
      throw new ExceptionInInitializerError(ex);
    }
  }

  public static EntityManagerFactory getEmf() {
    return emf;
  }
}

Exception Is:

Initial SessionFactory creation failed.javax.persistence.PersistenceException: No   Persistence provider for EntityManager named jspWithJpa
Mar 2, 2014 11:16:08 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [com.action.InsertData] in context with path   [/jspWithJpa] threw exception [Servlet execution threw an exception] with root cause
javax.persistence.PersistenceException: No Persistence provider for EntityManager named jspWithJpa

Solution

  • Change your provider tag to in persistence.xml to:

    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    

    from your libs i can see that you're using eclipselink as JPA provider, not hibernate.