Seperate persistence unit and application into different OSGI bundles in an Equinox environment

76 Views Asked by At

I have implemented a persistence unit including META-INF/persistence.xml in a separate bundle. Now I want to use this bundle in another plugin. However, I am not able to instantiate the EntityManagerFactory.

See below for the contents of the relevant files.

What works

If I put META-INF/persistence.xml into the application bundle than the following returns a valid EntityManagerFactory

EntityManagerFactory emf = new PersistenceProvider().createEntityManagerFactory("de.viate.muja.dao.jpa", <properties>);

What not works

The above line yields null if persistence.xml is only in the persistance bundle.

The EclipseLink documentation refers to Gemini with respect to OSGI. This site proposes the lookup using the OSGI services

context.getServiceReferences(EntityManagerFactory.class.getName(), "(osgi.unit.name=de.viate.muja.dao.jpa)");

This doesn't work too.

On the Gemini page there is a class EntityManagerFactoryBuilder. No idea where it is defined in OSGI context!?

What can I do to get a valid service reference? Which bundles are necessary in addition to those already imported (see below) and in which Eclipse (2022-03) feature are they included?

Alternatively: is there a way to import persistence.xml from the persistence bundle?

Persistence Bundle - MANIFEST.MG

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-SymbolicName: de.viate.muja.dao.bundle
Bundle-Version: 1.0.0
DynamicImport-Package: *
Export-Package: de.viate.muja.dao;version="1.0.0";uses:="jakarta.persi
 stence,jakarta.persistence.metamodel,org.eclipse.persistence.descript
 ors.changetracking,org.eclipse.persistence.indirection,org.eclipse.pe
 rsistence.internal.descriptors,org.eclipse.persistence.internal.ident
 itymaps,org.eclipse.persistence.internal.weaving,org.eclipse.persiste
 nce.queries,org.eclipse.persistence.sessions"
Import-Package: jakarta.persistence;resolution:=optional,jakarta.persi
 stence.metamodel;resolution:=optional,java.beans;resolution:=optional
 ,java.lang;resolution:=optional,java.lang.invoke;resolution:=optional
 ,java.util;resolution:=optional,java.util.function;resolution:=option
 al,java.util.stream;resolution:=optional,org.eclipse.persistence.anno
 tations;resolution:=optional,org.eclipse.persistence.descriptors.chan
 getracking;resolution:=optional,org.eclipse.persistence.indirection;r
 esolution:=optional,org.eclipse.persistence.internal.descriptors;reso
 lution:=optional,org.eclipse.persistence.internal.identitymaps;resolu
 tion:=optional,org.eclipse.persistence.internal.jpa;resolution:=optio
 nal,org.eclipse.persistence.internal.weaving;resolution:=optional,org
 .eclipse.persistence.queries;resolution:=optional,org.eclipse.persist
 ence.sessions;resolution:=optional
Meta-Persistence: META-INF/persistence.xml
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=17))"

Persistence Bundle - 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="de.viate.muja.dao.jpa" transaction-type="RESOURCE_LOCAL">

    <class>[omitted for brevity]</class>

    <shared-cache-mode>NONE</shared-cache-mode>

    <properties>
      <property name="eclipselink.weaving" value="static"/>
    </properties>
  </persistence-unit>
</persistence>

Application Bundle - MANIFEST.MF

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-SymbolicName: de.viate.muja.ui
Bundle-Version: 1.0.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-17
Bundle-ActivationPolicy: lazy
Require-Bundle: org.eclipse.osgi;bundle-version="3.17.200",
 org.osgi.service.jdbc;bundle-version="1.0.1",
 jakarta.persistence-api;bundle-version="3.0.0",
 org.eclipse.persistence.core;bundle-version="3.1.0",
 org.eclipse.persistence.asm;bundle-version="9.1.1",
 org.eclipse.persistence.jpa;bundle-version="3.1.0",
 org.eclipse.persistence.jpa.jpql;bundle-version="3.1.0",
 com.microsoft.sqlserver.mssql-jdbc;bundle-version="10.2.0",
 de.viate.muja.dao.bundle;bundle-version="1.0.0"
1

There are 1 best solutions below

2
BJ Hargrave On

Look at Aries JPA which implements the OSGi JPA Service Specification.