1

StackOverflow 上已经有很多关于此错误消息的问题,但我找不到解决方案...

错误是:

严重:将异常映射到响应:500(内部服务器错误)javax.persistence.PersistenceException:在 org.odata4j.producer.jpa 的 javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:84) 中没有名为 CreateJPA 的 EntityManager 的持久性提供程序。 JPAProducerFactory.create(JPAProducerFactory.java:32) at org.odata4j.producer.resources.DefaultODataProducerProvider.newProducerFromFactory(DefaultODataProducerProvider.java:113) at .....

我的 persistence.xml 是:

<?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>

它由这一行调用(没有错误):

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

我的 persistence.xml 在 Project Explorer 中的 JPA Content 下:

在此处输入图像描述

我的 pom.xml 是(也可以在这里查看): 在此处输入图像描述

CreateEntityManagerFactory 是:

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

更新:JPAFactory(在 createEntityManagerFactory 函数中没有属性值)

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

 }
4

1 回答 1

0

对于标准 JavaSE 应用程序,放置META-INF\persistence.xml在文件夹的根src目录中。

如果您使用的是 JEE 容器托管实体管理器,那么您肯定会使用JTA它作为您的事务类型,而不是RESOURCE_LOCAL. 然后确保WebContent->META-INF包含persistence.xml.

例如

<persistence-unit name="createJPA" transaction-type="JTA">
    <jta-data-source>jdbc/MyDatasource</jta-data-source>
</persistence-unit>
于 2014-03-01T16:22:17.883 回答