【问题标题】:Karaf and persistence unit: Missing dependencies: JpaTemplateKaraf 和持久性单元:缺少依赖项:JpaTemplate
【发布时间】:2017-06-26 09:36:59
【问题描述】:

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

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 中成功启动了持久单元。

【问题讨论】:

  • 你能给出你的persistence.xml的日志和内容吗?

标签: jpa osgi eclipselink karaf


【解决方案1】:

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

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

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

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

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

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

【讨论】:

  • 第一件事:是的 - 查看我的编辑> 下一步:读取配置文件,我在 WebConsole 中看到它。我的包在 Eclipse OSGi 框架启动配置中工作。现在我尝试为 Karaf 配置运行时环境。
  • 您正在使用来自 pax jdbc 的数据源配置。这意味着您还需要安装功能 pax-jdbc-config 和 pax-jdbc-sqlite。之后检查您的 ds 配置是否已读取并检查是否创建了数据源。
  • 谢谢。请包括您对 pax-jdbc-config 和 pax-jdbc-sqlite 的评论,因为在我的案例中这是缺少的部分。
猜你喜欢
  • 1970-01-01
  • 2020-02-12
  • 2012-07-16
  • 1970-01-01
  • 2021-05-18
  • 2017-12-25
  • 2015-04-18
  • 2020-03-24
  • 2012-07-12
相关资源
最近更新 更多