嵌入式Derby与JPA:没有为EntityManager命名的持久性提供程序

时间:2015-11-11 22:45:29

标签: eclipse jpa derby

又一个。 3.5小时后,我转向stackoverflow以查看是否有人可以找出发生此错误的原因。

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

完整的异常回溯如下

javax.persistence.PersistenceException: No Persistence provider for EntityManager named derby
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
    at mmis.mars.irs.mec.batch.DerbyTest.setup(DerbyTest.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

引用了以下罐子。这些是JPA,eclipselink,derby和derbyclient。我不认为derbyclient是需要的,但我在另一篇关于嵌入式德比与eclipselink的帖子中包含了它。

  • javax.persistence_2.1.0.v201304241213.jar
  • eclipselink.jar
  • 的derby.jar
  • derbyclient.jar中

以下是persistence.xml文件。它位于META-INF文件夹的内部,该文件夹位于项目的根目录中。 META-INF文件夹作为源文件夹添加到项目中。

    <?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="derby" transaction-type="RESOURCE_LOCAL">
            <class>JPA_class1</class>
            <class>JPA_class2</class>
            <class>JPA_class3</class>
            <class>JPA_class4</class>

            <properties>
                <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
                <property name="javax.persistence.jdbc.url" value="jdbc:derby:thedb;create=true" />
            </properties>
        </persistence-unit>
    </persistence>

META-INF包含

  • 包括:(全部)
  • 除外:(无)
  • 原生图书馆位置:(无)
  • 忽略可选的编译问题:(否)

根据其他帖子,不需要提供者标记。

我尝试将每个提供商代码包含在正确的位置。两者都没有。

<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

以下是我尝试使用提供者标签的内容。

<persistence-unit name="derby" transaction-type="RESOURCE_LOCAL">
    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>

    <class>...</class>

    <properties>
        ...
    </properties>
</persistence-unit>

<persistence-unit name="derby" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

    <class>...</class>

    <properties>
        ...
    </properties>
</persistence-unit>

我目前正在&#34;测试&#34;中的JUnit测试中运行它。文件夹,位于根目录中,也包含在源文件夹中。 JUnit测试失败,出现以下错误。

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

以下是导致错误的代码行。

Persistence.createEntityManagerFactory("derby").createEntityManager();

1 个答案:

答案 0 :(得分:0)

尝试验证JPA persistence.xml文件是否已命名并位于&#34; src / META-INF / persistence.xml&#34;在项目文件夹中。由于路径文件名不正确或persistence.xml文件的路径名不正确,JPA可能找不到持久性单元。