Karaf 和持久性单元:缺少依赖项:JpaTemplate

Karaf and persistence unit: Missing dependencies: JpaTemplate

我一直在努力安装 JPA 并在 Karaf 4.0.8 容器上启动持久性单元。我已经安装:

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

我的带有持久性单元的包启动了,但它没有导出任何服务。使用它并导入 JpaTemplate 的包无法导入服务:

<reference id="jpaTemplate" interface="org.apache.aries.jpa.template.JpaTemplate" filter="(osgi.unit.name=officialUnit)" />

应该从我的持久性包中导出,但它不是。 'diag' 显示持久性包没有问题。

如何调试持久化单元未正确启动的原因?

我的 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="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>

配置文件etc/org.ops4j.datasource-sqlitetest.cfg:

osgi.jdbc.driver.name=sqlite
url=jdbc:sqlite:D:/data/db/demo.db
dataSourceName=demo-db

以及持久性包的MANIFEST.MF

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

已使用 OSGi 框架启动配置在 Eclipse 中成功启动持久单元。

Jpa 调试起来并不简单,因为它有很多活动部件。第一步是检查 aries jpa 容器是否完全找到了您的持久性包。

首先要检查的是您的持久性单元包的清单是否具有 Meta-Persistence header 并指向 persistence.xml,例如:META-INF/persistence.xml.

如果你有这个,那么 jpa 容器应该获取包并解析你的 persistence.xml。

接下来要检查的是 PersistenceProvider 服务是否存在以及数据源是否存在(如果您使用数据源)。

如果您使用配置配置数据源,请不要忘记安装必要的 pax-jdbc 功能:

feature:repo-add pax-jdbc 1.0.0
feature:install pax-jdbc-config pax-jdbc-sqlite