Java JPA 异常:没有名为 MyJPAApplicationPU 的 EntityManager 的持久性提供程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3219994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:30:55  来源:igfitidea点击:

JPA Exception: No Persistence provider for EntityManager named MyJPAApplicationPU

javaormjpapersistence

提问by Muneeswaran Balasubramanian

I am newbie with JPA. I'm trying to run some sample code using JPA but I get the following exception:

我是 JPA 的新手。我正在尝试使用 JPA 运行一些示例代码,但出现以下异常:

javax.persistence.PersistenceException: No Persistence provider for EntityManager named MyJPAApplicationPU

I put my exception message here,

我把我的异常信息放在这里,

INFO: Could not find any META-INF/persistence.xml file in the classpath
javax.persistence.PersistenceException: No Persistence provider for EntityManager named MyJPAApplicationPU
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
        at com.myJpa.TestJPA.setUp(TestJPA.java:30)
        at com.myJpa.TestJPA.main(TestJPA.java:72)

Any help would be appreciated.

任何帮助,将不胜感激。

采纳答案by Pascal Thivent

Well, the error is self explaining, you need to provide a META-INF/persistence.xmlto use JPA. This file is used to define a "persistence unit". From the JPA 1.0 specification:

好吧,错误是自我解释的,您需要提供一个META-INF/persistence.xml才能使用 JPA。该文件用于定义“持久性单元”。来自 JPA 1.0 规范:

6.2.1 persistence.xml file

A persistence.xmlfile defines a persistence unit. It may be used to specify managed persistence classes included in the persistence unit, object/relational mapping information for those classes, and other configuration information for the persistence unit and for the entity manager(s) and entity manager factory for the persistence unit. The persistence.xmlfile is located in the META-INFdirectory of the root of the persistence unit. This information may be defined by containment or by reference, as described below.

6.2.1persistence.xml文件

一个persistence.xml文件定义了一个持久化单元。它可用于指定持久性单元中包含的受管持久性类、这些类的对象/关系映射信息,以及持久性单元和持久性单元的实体管理器和实体管理器工厂的其他配置信息。该 persistence.xml文件位于META-INF持久性单元的根目录中。该信息可以通过包含或参考来定义,如下所述。

Here is a sample persistence.xmlfor a Java SE environment (using Hibernate as JPA provider):

这是persistence.xmlJava SE 环境的示例(使用 Hibernate 作为 JPA 提供程序):

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
  version="1.0">
    <persistence-unit name="MyJPAApplicationPU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>com.mycompany.Foo</class>
        <class>com.mycompany.Bar</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.connection.provider_class" value="org.hibernate.connection.DriverManagerConnectionProvider" />
            <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:unit-testing-jpa"/>
            <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
            <property name="hibernate.connection.username" value="sa"/>
            <property name="hibernate.connection.password" value=""/>
            <property name="hibernate.show_sql" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

Some comments about the above file:

关于上述文件的一些评论:

  • when running in a Java SE environment, you cannot rely on JTA and the transaction type must be RESOURCE_LOCAL(which is actually the default in a Java SE environment but specifying it make it more clear).
  • when running in a Java SE environment, you cannot use a JDNI data source and the provider will obtain connections directly from the JDBC driver so you must pass the relevant informations to the provider (driver class name, connection url, user, password). With JPA 1.0 the properties used to pass these metadata are provider specific.
  • To insure the portability of a Java SE application, it is necessary to explicitly listthe managed persistence classes that are included in the persistence unit.
  • 在 Java SE 环境中运行时,不能依赖 JTA,事务类型必须是RESOURCE_LOCAL(这实际上是 Java SE 环境中的默认值,但指定它会更清楚)。
  • 在 Java SE 环境中运行时,不能使用 JDNI 数据源,提供程序将直接从 JDBC 驱动程序获取连接,因此您必须将相关信息(驱动程序类名、连接 url、用户、密码)传递给提供程序。在 JPA 1.0 中,用于传递这些元数据的属性是特定于提供者的。
  • 为了确保 Java SE 应用程序的可移植性,有必要明确列出持久性单元中包含的托管持久性类。

回答by Bozho

For JPA to work, you need META-INF/persistence.xml. I will assume this is a web-app, so this folder has to be in WEB-INF/classes/.

要使 JPA 正常工作,您需要META-INF/persistence.xml. 我会假设这是一个网络应用程序,所以这个文件夹必须在 WEB-INF/classes/.

The persistence.xmlfile would look like this:

persistence.xml文件将如下所示:

<persistence 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"
        version="1.0">

  <persistence-unit name="MyJPAApplicationPU" transaction-type="RESOURCE_LOCAL">

  </persistence-unit>

</persistence>

回答by Reegan Miranda

 EntityManagerFactory emf = Persistence.createEntityManagerFactory("<JDBC connection>");

Check the correct JDBC Connection.

检查正确的 JDBC 连接。