1
votes

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;
  }

 }
1
I think you should have persistence.xml in META-INF folder.Rohit Jain
I just quickly tried tried that - no change in the error.AllieCat
Can you show your pom.xml?Rohit Jain
Added. I'm having trouble adding it is code - I put it up here: docs.google.com/document/d/…AllieCat
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 <provider> tag in the persistence.xml file, and everything works fine for me.Igor Rodriguez

1 Answers

0
votes

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>