如何使用EclipseLink使PostgreSQL与OSGi一起工作

我需要使用EclipseLink开发一个OSGi包,它应该连接到PostgreSQL,但是我浪费了很多时间并且无法使其工作.我收到这个“javax.persistence.PersistenceException:没有为EntityManager命名的房间的持久性提供程序”.

基本上,我按照我在网上找到的教程的步骤.我使用Eclipse Indigo

Manifest file : 
Manifest-Version: 1.0
Meta-Persistence: persistence.xml
JPA-PersistenceUnits: rooms
Bundle-ClassPath: ., META-INF/persistence.xml
Bundle-ManifestVersion: 2
Bundle-Name: PMSTestTask
Bundle-SymbolicName: PMSTestTask
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: pmstesttask.Activator
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: javax.persistence;version="2.0.3", org.osgi.framework;version="1.3.0", org.postgresql
Require-Bundle: org.eclipse.persistence.jpa;bundle-version="2.1.0", org.eclipse.persistence.jpa.osgi;bundle-version="2.3.0"

persistence.xml中

 <?xml version="1.0" encoding="UTF-8" ?>
 <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"
    version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">

    <persistence-unit name="rooms" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>test_task.domain.Room</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
            <property name="javax.persistence.jdbc.url"
                value="jdbc:postgresql://localhost:5432/Hotel_test_task" />
            <property name="javax.persistence.jdbc.user" value="" />
            <property name="javax.persistence.jdbc.password" value="" />

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

Activator.java

 EntityManagerFactory emf = Persistence
                .createEntityManagerFactory("rooms");
 EntityManager em = emf.createEntityManager();

更新. 0

添加了必需的bundle to manifest.得到了这个

org.osgi.framework.BundleException: The bundle "org.eclipse.persistence.jpa.osgi_2.3.0.v20110604-r9504 [26]" could not be resolved. Reason: Missing Constraint: Require-Bundle: org.eclipse.persistence.core; bundle-version="2.3.0"

解决方法:

由于您自己实例化EMF(并且不使用例如Enterprise OSGi来注入EMF),您必须将EclipseLink类添加到bundle的类路径中,否则Java无法实例化指定的< provider>类,可能会导致此错误消息.

请参阅LazyLoadingRCP示例,尤其是org.eclipse.persistence.example.jpa.rcp.comics包的类加载器说明,或该包的具体MANIFEST.MF(Eclipse链接包含在Require-Bundle中).

上一篇:java – 在构造函数中初始化public static final变量


下一篇:VS2019中解决窗体程序模糊的问题