没有名为 X 的 EntityManager 的持久性提供程序 [英] No Persistence provider for EntityManager named X

查看:23
本文介绍了没有名为 X 的 EntityManager 的持久性提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 JPA 开发 JavaSE 应用程序.不幸的是,我在调用后得到 null:Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);

I am developing a JavaSE application using JPA. Unfortunately, I get null after calling: Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);

下面你会发现:

  • 我调用 EntityManagerFactory 并意外返回 null
  • 的代码片段
  • 我的persistence.xml文件
  • 我的项目结构

我的代码片段:

public class Main {
    private static final String PERSISTENCE_UNIT_NAME = "MeineJpaPU";
    private static EntityManagerFactory factory;

    public static void main(String[] args) {
        // I get null on this line!!!
       factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);

       EntityManager em = factory.createEntityManager();
       // do stuff with entity manager
       ...
    }
}

我的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
  <persistence-unit name="MeineJpaPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
    <class>path.to.package.server.Todo</class>
      <exclude-unlisted-classes>false</exclude-unlisted-classes>
     <properties>       
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>       
            <property name="javax.persistence.jdbc.url"  value="jdbc:postgresql://localhost:5432/test"/>       
            <property name="javax.persistence.jdbc.user" value="postgres"/>       
            <property name="javax.persistence.jdbc.password" value="postgres"/>       
        </properties>
  </persistence-unit>
</persistence>

我的项目结构:

推荐答案

您必须将 persistence.xml 文件移动到适当的位置.

You must move persistence.xml file to an appropriate location.

更具体地说,将 META-INF/persistence.xml 文件添加到源文件夹的根目录.

More specifically, add META-INF/persistence.xml file to the root of a source folder.

在这种情况下,以下是一个合适的位置:srcmainjavaMETA-INFpersistence.xml

In this case, the following is an appropriate location: srcmainjavaMETA-INFpersistence.xml

详情如下:(取自 JPA 规范)

Here are the details: (taken from the JPA spec)

persistence.xml 文件定义了一个持久性单元.持久性.xml文件位于根目录的 META-INF 目录中持久化单元.

A persistence.xml file defines a persistence unit. The persistence.xml file is located in the META-INF directory of the root of the persistence unit.

持久化单元的根是这里的关键.

The root of the persistence unit is the key here.

如果您是非 Java EE 应用

jar 文件或目录,其 META-INF 目录包含persistence.xml 文件被称为持久化单元的根.

The jar file or directory whose META-INF directory contains the persistence.xml file is termed the root of the persistence unit.

如果您使用的是 Java EE 应用,则以下内容有效

If you are in a Java EE app, the following are valid

在 Java EE 环境中,持久性单元的根必须是以下内容:

In Java EE environments, the root of a persistence unit must be one of the following:

  • 一个 EJB-JAR 文件
  • WAR 文件的 WEB-INF/classes 目录[80]
  • WAR 文件的 WEB-INF/lib 目录中的 jar 文件
  • EAR 库目录中的一个 jar 文件
  • 一个应用程序客户端 jar 文件

这篇关于没有名为 X 的 EntityManager 的持久性提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆