1

There are so many questions about this error message already on StackOverflow, but I can't find a solution...

The error is:

SEVERE: Mapped exception to response: 500 (Internal Server Error) javax.persistence.PersistenceException: No Persistence provider for EntityManager named CreateJPA at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:84) at org.odata4j.producer.jpa.JPAProducerFactory.create(JPAProducerFactory.java:32) at org.odata4j.producer.resources.DefaultODataProducerProvider.newProducerFromFactory(DefaultODataProducerProvider.java:113) at ........

My persistence.xml is:

<?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="createJPA" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>model.Cumulative</class>
    <class>model.Date</class>
    <class>model.Department</class>
    <class>model.Holiday</class>
    <class>model.Log</class>
    <class>model.Member</class>
    <class>model.Person</class>
    <class>model.Project</class>
    <properties>
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/resource_db"/>
        <property name="javax.persistence.jdbc.user" value="root"/>
        <property name="javax. persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        <property name="javax.persistence.jdbc.password" value=""/>
    </properties>
</persistence-unit>
</persistence>

It is called by this line (which has no errors):

EntityManagerFactory emf = Persistence.createEntityManagerFactory("createJPA", properties);

My persistence.xml is under JPA Content in the Project Explorer:

enter image description here

My pom.xml is (also viewable here): enter image description here

CreateEntityManagerFactory is:

 public static EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties) {

    EntityManagerFactory emf = null;
    PersistenceProviderResolver resolver = PersistenceProviderResolverHolder.getPersistenceProviderResolver();

    List<PersistenceProvider> providers = resolver.getPersistenceProviders();

    for (PersistenceProvider provider : providers) {
        emf = provider.createEntityManagerFactory(persistenceUnitName, properties);
        if (emf != null) {
            break;
        }
    }
    if (emf == null) {
        throw new PersistenceException("No Persistence provider for EntityManager named " + persistenceUnitName);
    }
    return emf;
}

UPDATE: JPAFactory (with no properties value in createEntityManagerFactory function)

package org.odata4j.producer.jpa;

import java.util.Properties;
import java.util.logging.Logger;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.odata4j.producer.ODataProducer;
import org.odata4j.producer.ODataProducerFactory;

public class JPAProducerFactory implements ODataProducerFactory {

  private final Logger log = Logger.getLogger(getClass().getName());

  public static final String PUNAME_PROPNAME = "odata4j.jpa.persistenceUnitName";
  public static final String NAMESPACE_PROPNAME = "odata4j.jpa.edmNamespace";
  public static final String MAX_RESULTS_PROPNAME = "odata4j.jpa.maxResults";

  @Override
   public ODataProducer create(Properties properties) {

  String persistenceUnitName = properties.getProperty(PUNAME_PROPNAME);
    if (persistenceUnitName == null || persistenceUnitName.length() == 0)
      throw new RuntimeException("Missing required property: " + PUNAME_PROPNAME);

    String edmNamespace = properties.getProperty(NAMESPACE_PROPNAME, "");
    String maxResults = properties.getProperty(MAX_RESULTS_PROPNAME, "50");

    log.info(String.format("Using persistence unit [%s] with edm namespace [%s] and max results [%s]", persistenceUnitName, edmNamespace, maxResults));
    log.info("Persistence name is:" + persistenceUnitName);
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("createJPA");
    JPAProducer producer = new JPAProducer(emf, edmNamespace, Integer.parseInt(maxResults));
    return producer;
  }

 }
AllieCat
  • 3,890
  • 10
  • 31
  • 45
  • 1
    I think you should have `persistence.xml` in `META-INF` folder. – Rohit Jain Jun 24 '13 at 17:57
  • I just quickly tried tried that - no change in the error. – AllieCat Jun 24 '13 at 17:59
  • Can you show your `pom.xml`? – Rohit Jain Jun 24 '13 at 18:02
  • Added. I'm having trouble adding it is code - I put it up here: https://docs.google.com/document/d/1MWu0_Sq-0Eiy2vRujQ7ZYSkyRKSRZF6YE1SmxG3BySs/edit?usp=sharing – AllieCat Jun 24 '13 at 18:11
  • What is the properties variable do when declaring the EntityManager? Have you tried without them? On the other side, in my experiments, I didn't specify none tag in the persistence.xml file, and everything works fine for me. – Igor Rodriguez Jun 24 '13 at 18:20
  • Can you show some code surrounding the `createEntityManagerFactory`? – Rohit Jain Jun 24 '13 at 18:22
  • @RohitJain, I added the createEntityManagerFactory function. – AllieCat Jun 24 '13 at 18:35
  • @IgorRodriguez, I just tried without the properties variable and it had the same error. What do you mean "everything works fine for me"? – AllieCat Jun 24 '13 at 18:37
  • Why are you using `PersistenceProviderResolverHolder`?? You can simply do - `Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);`, wherever you want. Seems like you have created your own method. Just try to remove it. – Rohit Jain Jun 24 '13 at 18:44
  • Okay - I'll try editing it and get back to you. The function createEntityManagerFactory is actually an include file right now - from the opensource project odata4j. – AllieCat Jun 24 '13 at 18:47
  • Try to use the createEntityManagerFactory method from javax.persistence package, to check whether the problem is related. It looks that the problem is more related with the persistence.xml file though. Try removing the line: "org.eclipse.persistence.jpa.PersistenceProvider". – Igor Rodriguez Jun 24 '13 at 18:50
  • @IgorRodriguez. That(provider) line might not be giving problem. I think OP is using some other `createEntityManagerFactory` method. – Rohit Jain Jun 24 '13 at 18:58
  • Hang on. I just realized the createEntityManagerFactory is actually provided by Oracle. It has "* Copyright (c) 2008 - 2013 Oracle Corporation." on the top. Should I really be editing this? – AllieCat Jun 24 '13 at 19:01
  • @IgorRodriguez - removed the line. No change – AllieCat Jun 24 '13 at 19:01
  • No, you shouldn't edit it. But that same factory needs to have another method, that takes only a String argument, the name of the persistence unit, instead of two, so that it results in EntityManagerFactory emf = Persistence.createEntityManagerFactory("createJPA"); – Igor Rodriguez Jun 24 '13 at 19:03
  • @AllieCat. You should not edit the `createEntityManagerFactory` method. You should use the `createEntityManagerFactory` method defined in `javax.persistence.Persistence`. – Rohit Jain Jun 24 '13 at 19:04
  • @AllieCat. Check your import statement, to see which `Persistence` class is being imported? – Rohit Jain Jun 24 '13 at 19:04
  • @RohitJain, I am using the createEntityManagerFactory method in javax.persistence.Persistence.... – AllieCat Jun 24 '13 at 19:06
  • @AllieCat. Remove the 2nd argument from there. As pointed out by Igor. – Rohit Jain Jun 24 '13 at 19:06
  • I removed the second argument and edited my post (at the bottom) that has the entire script. The error hasn't changed. Are we sure it's a code error and I haven't left out a dependency or something? – AllieCat Jun 24 '13 at 19:11
  • @AllieCat. Did you add ` line back again in `persistence.xml`. – Rohit Jain Jun 24 '13 at 19:14
  • No. Persistence.xml doesn't have the line any more. – AllieCat Jun 24 '13 at 19:15
  • It's still throwing in error. Hang on - I'm just gonna restart my computer. lol – AllieCat Jun 24 '13 at 19:19
  • Well my entire JPAContext with the persistence.xml file has now disappeared. I guess that explains it!!! I'm just going to regenerate it. :( But thank you guys so much. – AllieCat Jun 24 '13 at 19:25
  • @AllieCat. That might be because, JPA facet has been removed from your project. That's no problem. Did you move persistence.xml to `META-INF`? If not. Move it there. – Rohit Jain Jun 24 '13 at 19:26
  • The JPA facet has been removed. But persistence.xml is still in the META-INF. Why was the facet removed?? – AllieCat Jun 24 '13 at 19:46
  • Is your `META-INF` folder inside your `src` folder? Try moving it there. – Rohit Jain Jun 24 '13 at 20:37
  • possible duplicate of [No Persistence provider for EntityManager named X](http://stackoverflow.com/questions/19322827/no-persistence-provider-for-entitymanager-named-x) – cmd Mar 01 '14 at 16:18

1 Answers1

0

For a standard JavaSE application, place META-INF\persistence.xml in the root of your src folder.

If you are using JEE container managed entity manager, you sure use JTA as your transaction type instead of RESOURCE_LOCAL. Then ensure WebContent->META-INF contains persistence.xml.

e.g.

<persistence-unit name="createJPA" transaction-type="JTA">
    <jta-data-source>jdbc/MyDatasource</jta-data-source>
</persistence-unit>
cmd
  • 11,622
  • 7
  • 51
  • 61