I was fighting to install JPA and start persistence unit on Karaf 4.0.8 container. I've installed:
feature:install webconsole
feature:repo-add mvn:org.apache.aries.jpa/jpa-features/2.5.0/xml/features
feature:install jpa/2.5.0
feature:install transaction-api/1.2.0
feature:install transaction
feature:install eclipselink
My bundle with persistence unit starts, but it doesn't export any services. The bundle that uses it and imports JpaTemplate fails to import service:
<reference id="jpaTemplate" interface="org.apache.aries.jpa.template.JpaTemplate" filter="(osgi.unit.name=officialUnit)" />
which should be exported from my persistence bundle, but it is not. 'diag' shows no problem with persistence bundle.
How to debug why the persistence unit is not correctly started?
My persistence.xml looks like that:
<?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="officialUnit" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<!-- Only used when transaction-type=JTA -->
<jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=demo-db)</jta-data-source>
<!-- Only used when transaction-type=RESOURCE_LOCAL -->
<non-jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=demo-db)</non-jta-data-source>
<class>example.entity.SomeEntity</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.ddl-generation" value="none" />
<property name="eclipselink.ddl-generation.output-mode" value="sql-script" />
</properties>
</persistence-unit>
</persistence>
The configuration file etc/org.ops4j.datasource-sqlitetest.cfg
:
osgi.jdbc.driver.name=sqlite
url=jdbc:sqlite:D:/data/db/demo.db
dataSourceName=demo-db
And the MANIFEST.MF
of persistence bundle:
Manifest-Version: 1.0
Bnd-LastModified: 1486560203107
Build-Jdk: 1.8.0_121
Bundle-Description: Persistence
Bundle-ManifestVersion: 2
Bundle-Name: example.persistence.impl
Bundle-SymbolicName: example.persistence.impl
Bundle-Version: 1.0.0.SNAPSHOT
Created-By: Apache Maven Bundle Plugin
Export-Package: example.persistence.impl;version="1.0.0";uses:="javax.pe
rsistence,javax.xml.bind.annotation"
Import-Package: javax.persistence;version="[2.1,3)",javax.xml.bind.annot
ation
Meta-Persistence: META-INF/persistence.xml
Originally-Created-By: Apache Maven Bundle Plugin
Provide-Capability: osgi.service;effective:=active;objectClass="org.apac
he.aries.jpa.supplier.EmSupplier";osgi.unit.name=officialUnit,osgi.serv
ice;effective:=active;objectClass="javax.persistence.EntityManagerFacto
ry";osgi.unit.name=officialUnit,osgi.service;effective:=active;objectCl
ass="org.apache.aries.jpa.template.JpaTemplate";osgi.unit.name=official
Unit,osgi.service;effective:=active;objectClass="javax.persistence.Enti
tyManager";osgi.unit.name=officialUnit
Require-Capability: osgi.service;effective:=active;objectClass="javax.sq
l.DataSource";filter:="(osgi.jndi.service.name=demo-db)",osgi.service;e
ffective:=active;objectClass="javax.persistence.spi.PersistenceProvider
";javax.persistence.provider="org.eclipse.persistence.jpa.PersistencePr
ovider",osgi.extender;osgi.extender="aries.jpa",osgi.service;effective:
=active;objectClass="javax.transaction.TransactionManager",osgi.ee;filt
er:="(&(osgi.ee=JavaSE)(version=1.8))"
Tool: Bnd-3.2.0.201605172007
The persistent unit was successfully started in Eclipse using OSGi Framework launch configuration.
Jpa is not trivial to debug as there are many moving parts. The first step is to check if your persistence bundle is found by aries jpa container at all.
First thing to check is if the Manifest of your persitence unit bundle has the Meta-Persistence header and points to the persistence.xml like: META-INF/persistence.xml.
If you have this then jpa container should pick up the bundle and parse your persistence.xml.
Next things to check is if the PersistenceProvider service is present and if the DataSource is present (if you use a DataSource).
If you configure your DataSource using a config then do not forget to install the necessary pax-jdbc features:
feature:repo-add pax-jdbc 1.0.0
feature:install pax-jdbc-config pax-jdbc-sqlite