【问题标题】:JPA with Java SE: javax.persistence.PersistenceException: No Persistence provider for EntityManager带有 Java SE 的 JPA:javax.persistence.PersistenceException:EntityManager 没有持久性提供程序
【发布时间】:2017-04-04 05:56:22
【问题描述】:

执行 Java SE JPA 独立应用程序会引发异常:

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

我发现了几篇类似的帖子,但我认为这些帖子都不适用于我使用 EclipseLink、Java SE 的情况,并且没有打包到可部署的文件中,因此没有 WEB-INF 等。

/src/META-INF/persistence.xml 是:

<persistence>
    <persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>SomeEntity</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/somedbname"/>
            <property name="javax.persistence.jdbc.user" value="foo"/>
            <property name="javax.persistence.jdbc.password" value="bar"/>
        </properties>
    </persistence-unit>
</persistence>

以下 jars 在构建路径上:

  • org.eclipse.persistence.jpa-2.6.4.jar
  • eclipselink-2.6.4.jar
  • javax.persistence-2.1.1.jar
  • postgresql-9.4.1212.jar

【问题讨论】:

    标签: java jpa persistence eclipselink


    【解决方案1】:

    我必须在 persistence.xml 中提供带有 xmlns 属性的持久性命名空间

    <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
    

    【讨论】:

    • 我不知道为什么,但就我而言,这是解决方案。在我的 Tomcat 项目中,我使用了一个标签:&lt;persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"&gt;,但在这个独立的应用程序中,我必须将 xmlnsversion 更改为 @JCvanDamme 所说的...
    【解决方案2】:

    您必须将 eclipselink jar 添加到您的 ClassPath 以检查它是否已经存在:

    System.out.println(System.getProperty("java.class.path"));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-03
      • 2013-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多