OSGi + JPA(postgresql)

发布于 2024-12-12 05:11:31 字数 6269 浏览 0 评论 0原文

我尝试在 OSGi 中使用 JPA 并遇到此异常:

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

META-INF 中有简单的实体类,文件“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="test" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>org.test.Room</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/test"/>
      <property name="javax.persistence.jdbc.password" value="12345"/>
      <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
      <property name="javax.persistence.jdbc.user" value="postgres"/>

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

Activator:

package org.test;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {

    private static BundleContext context;
    static BundleContext getContext() {
        return context;
    }

    public void start(BundleContext bundleContext) throws Exception {
        Activator.context = bundleContext;
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("test");
    }

    public void stop(BundleContext bundleContext) throws Exception {
        Activator.context = null;
    }
}

Manifest.mf:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Test
Bundle-SymbolicName: org.test
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: org.test.Activator
Import-Package: javax.persistence;version="2.0.3", 
 org.osgi.framework;version="1.3.0",
 org.postgresql;version="8.3.604"
Bundle-RequiredExecutionEnvironment: JavaSE-1.7

和错误:

org.osgi.framework.BundleException:异常 包 org.test 的 org.test.Activator.start()。在

org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:734)

<块引用>

在 org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:683) 在 org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:381) 在 org.eclipse.osgi.framework.internal.core.AbstractBundle.resume(AbstractBundle.java:389) 在 org.eclipse.osgi.framework.internal.core.Framework.resumeBundle(Framework.java:1131) 在 org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:559) 在 org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:544) 在 org.eclipse.osgi.framework.internal.core.StartLevelManager.incFWSL(StartLevelManager.java:457) 在 org.eclipse.osgi.framework.internal.core.StartLevelManager.doSetStartLevel(StartLevelManager.java:243) 在 org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:438) 在 org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:1) 在 org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230) 在 org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340) 引起原因:javax.persistence.PersistenceException:无持久性 EntityManager 的提供者名为 test at javax.persistence.Persistence.createEntityManagerFactory(未知 来源)位于 javax.persistence.Persistence.createEntityManagerFactory(未知 来源)位于 org.test.Activator.start(Activator.java:23) 处 org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(BundleContextImpl.java:711) 在 java.security.AccessController.doPrivileged(本机方法)处 org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:702) ... 12个更多根异常: javax.persistence.PersistenceException:没有名为 test 的 EntityManager 的持久性提供程序 javax.persistence.Persistence.createEntityManagerFactory(未知 来源)位于 javax.persistence.Persistence.createEntityManagerFactory(未知 来源)位于 org.test.Activator.start(Activator.java:23) 处 org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(BundleContextImpl.java:711) 在 java.security.AccessController.doPrivileged(本机方法)处 org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:702) 在 org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:683) 在 org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:381) 在 org.eclipse.osgi.framework.internal.core.AbstractBundle.resume(AbstractBundle.java:389) 在 org.eclipse.osgi.framework.internal.core.Framework.resumeBundle(Framework.java:1131) 在 org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:559) 在 org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:544) 在 org.eclipse.osgi.framework.internal.core.StartLevelManager.incFWSL(StartLevelManager.java:457) 在 org.eclipse.osgi.framework.internal.core.StartLevelManager.doSetStartLevel(StartLevelManager.java:243) 在 org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:438) 在 org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:1) 在 org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230) 在 org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)

persistence.xml 在一个简单的 java 项目中可以正常工作,但是在 OSGi 插件中我遇到了这个问题。怎么了?

I tried to use JPA in OSGi and faced with this exception:

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

There are simple entity class, file "persistence.xml" in 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="test" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>org.test.Room</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/test"/>
      <property name="javax.persistence.jdbc.password" value="12345"/>
      <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
      <property name="javax.persistence.jdbc.user" value="postgres"/>

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

Activator:

package org.test;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {

    private static BundleContext context;
    static BundleContext getContext() {
        return context;
    }

    public void start(BundleContext bundleContext) throws Exception {
        Activator.context = bundleContext;
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("test");
    }

    public void stop(BundleContext bundleContext) throws Exception {
        Activator.context = null;
    }
}

Manifest.mf:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Test
Bundle-SymbolicName: org.test
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: org.test.Activator
Import-Package: javax.persistence;version="2.0.3", 
 org.osgi.framework;version="1.3.0",
 org.postgresql;version="8.3.604"
Bundle-RequiredExecutionEnvironment: JavaSE-1.7

And error:

org.osgi.framework.BundleException: Exception in
org.test.Activator.start() of bundle org.test. at

org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:734)

at
org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:683)
at
org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:381)
at
org.eclipse.osgi.framework.internal.core.AbstractBundle.resume(AbstractBundle.java:389)
at
org.eclipse.osgi.framework.internal.core.Framework.resumeBundle(Framework.java:1131)
at
org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:559)
at
org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:544)
at
org.eclipse.osgi.framework.internal.core.StartLevelManager.incFWSL(StartLevelManager.java:457)
at
org.eclipse.osgi.framework.internal.core.StartLevelManager.doSetStartLevel(StartLevelManager.java:243)
at
org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:438)
at
org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:1)
at
org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
at
org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)
Caused by: javax.persistence.PersistenceException: No Persistence
provider for EntityManager named test at
javax.persistence.Persistence.createEntityManagerFactory(Unknown
Source) at
javax.persistence.Persistence.createEntityManagerFactory(Unknown
Source) at org.test.Activator.start(Activator.java:23) at
org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(BundleContextImpl.java:711)
at java.security.AccessController.doPrivileged(Native Method) at
org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:702)
... 12 more Root exception:
javax.persistence.PersistenceException: No Persistence provider for EntityManager named test at
javax.persistence.Persistence.createEntityManagerFactory(Unknown
Source) at
javax.persistence.Persistence.createEntityManagerFactory(Unknown
Source) at org.test.Activator.start(Activator.java:23) at
org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(BundleContextImpl.java:711)
at java.security.AccessController.doPrivileged(Native Method) at
org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:702)
at
org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:683)
at
org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:381)
at
org.eclipse.osgi.framework.internal.core.AbstractBundle.resume(AbstractBundle.java:389)
at
org.eclipse.osgi.framework.internal.core.Framework.resumeBundle(Framework.java:1131)
at
org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:559)
at
org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:544)
at
org.eclipse.osgi.framework.internal.core.StartLevelManager.incFWSL(StartLevelManager.java:457)
at
org.eclipse.osgi.framework.internal.core.StartLevelManager.doSetStartLevel(StartLevelManager.java:243)
at
org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:438)
at
org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:1)
at
org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
at
org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)

persistence.xml is works correctly in a simple java project, but in OSGi plug-in I have this problem. What's wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捎一片雪花 2024-12-19 05:11:31

您需要将 persistence.xml 路径添加为 MANIFEST.MF 标头,如下所示:

Meta-Persistence: META-INF/persistence.xml

尽管对于 EclipseLink,可能需要以下标头(标头值是逗号分隔的 PU 名称列表):

JPA-PersistenceUnits: test

You need to add the persistence.xml path as a MANIFEST.MF header, like so:

Meta-Persistence: META-INF/persistence.xml

Though for EclipseLink the following header may be necessary (header value is comma separated list of PU names):

JPA-PersistenceUnits: test
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文