简体   繁体   中英

Error: No Persistence provider for EntityManager

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:

在此处输入图片说明

My pom.xml is (also viewable 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;
  }

 }

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 .

eg

<persistence-unit name="createJPA" transaction-type="JTA">
    <jta-data-source>jdbc/MyDatasource</jta-data-source>
</persistence-unit>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM