使用 Hibernate 的 Karaf 不会启动我的持久性单元

Posted

技术标签:

【中文标题】使用 Hibernate 的 Karaf 不会启动我的持久性单元【英文标题】:Karaf with Hibernate does not start my persistence unit 【发布时间】:2015-01-27 23:45:50 【问题描述】:

我需要一些帮助来解决为什么我的持久性单元没有使用 Hibernate 加载到 Karaf 中。日志文件报告

Bundle com.vogelware.simple.remote.combined is waiting for dependencies [(&(&(org.apache.aries.jpa.proxy.factory=true)(osgi.unit.name=person)(objectClass=javax.persistence.EntityManagerFactory))].

我认为问题在于 EntityManagerFactory 服务没有运行。根据 SO 消息 Jpa-hibernate error while starting the service,所有必需的服务都在运行。见下文。

我尝试在休眠包之前启动我的持久性包,但得到了相同的行为。

下面列出的是数据源、持久性和持久性.xml 的蓝图。

关于如何运行这个简单的例子有什么建议吗?

已安装版本的功能和捆绑包

karaf@root> feature:list -i
Name        | Version          | Installed | Repository              | Description
-------------------------------------------------------------------------------------------------------------------------
standard    | 3.0.1            | x         | standard-3.0.1          | Karaf standard feature
config      | 3.0.1            | x         | standard-3.0.1          | Provide OSGi ConfigAdmin support
region      | 3.0.1            | x         | standard-3.0.1          | Provide Region Support
package     | 3.0.1            | x         | standard-3.0.1          | Package commands and mbeans
http        | 3.0.1            | x         | standard-3.0.1          | Implementation of the OSGI HTTP Service
kar         | 3.0.1            | x         | standard-3.0.1          | Provide KAR (KARaf archive) support
ssh         | 3.0.1            | x         | standard-3.0.1          | Provide a SSHd server on Karaf
management  | 3.0.1            | x         | standard-3.0.1          | Provide a JMX MBeanServer and a set of MBeans in K
transaction | 1.0.1            | x         | enterprise-3.0.1        | OSGi Transaction Manager
jpa         | 1.0.1            | x         | enterprise-3.0.1        | OSGi Persistence Container
hibernate   | 4.3.1.Final      | x         | enterprise-3.0.1        | Hibernate 4.3.x JPA persistence engine support
jndi        | 3.0.1            | x         | enterprise-3.0.1        | OSGi Service Registry JNDI access
pax-jetty   | 8.1.14.v20131031 | x         | org.ops4j.pax.web-3.1.0 | Provide Jetty engine support
pax-http    | 3.1.0            | x         | org.ops4j.pax.web-3.1.0 | Implementation of the OSGI HTTP Service

karaf@root> bundle:list
START LEVEL 100 , List Threshold: 50
 ID | State       | Lvl | Version               | Name
---------------------------------------------------------------------------------------
 86 | Active      |  80 | 3.0.1                 | Apache Karaf :: JNDI :: Command
114 | Active      |  80 | 2.7.7.5               | Apache ServiceMix :: Bundles :: antlr
115 | Active      |  80 | 1.8.2.2               | Apache ServiceMix :: Bundles :: ant
116 | Active      |  80 | 1.6.1.5               | Apache ServiceMix :: Bundles :: dom4j
117 | Active      |  80 | 1.14.1.1              | Apache ServiceMix :: Bundles :: serp
118 | Active      |  80 | 0.9.0                 | ClassMate
119 | Active      |  80 | 3.18.1.GA             | Javassist
120 | Active      |  80 | 1.0.2.Final           | JACC 1.4 API
121 | Active      |  80 | 0                     | wrap_mvn_org.jboss_jandex_1.1.0.Final
122 | Active      |  80 | 3.1.4.GA              | JBoss Logging 3
123 | Active      |  80 | 4.0.4.Final           | hibernate-commons-annotations
124 | Active      | 100 | 4.3.1.Final           | hibernate-core
125 | Active      | 100 | 4.3.1.Final           | hibernate-entitymanager
126 | Active      | 100 | 4.3.1.Final           | hibernate-osgi
127 | Active      |  80 | 10.10.1000001.1458268 | Apache Derby 10.10
128 | Active      |  80 | 0.0.0                 | derby-datasource.xml
129 | GracePeriod |  80 | 0.1.0.201411290933    | Remote with JPA
130 | Active      |  80 | 0.1.0.201411280737    | Simple Model API

为 EntityManager 运行服务

karaf@root(service)> list javax.transaction.TransactionManager
[org.apache.aries.transaction.AriesTransactionManager, javax.transaction.TransactionManager, javax.transaction.TransactionSynchroniz
ationRegistry, javax.transaction.UserTransaction, org.apache.geronimo.transaction.manager.RecoverableTransactionManager]
------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
 service.id = 662
Provided by :
 Apache Aries Transaction Manager (93)
Used by:
 Aries JPA Container Managed Contexts (91)

karaf@root(service)> list DataSource
[javax.sql.DataSource]
----------------------
 osgi.jndi.service.name = jdbc/MotorDB
 osgi.service.blueprint.compname = MotorDB
 service.id = 713
Provided by :
 Bundle 130 (my datasource bundle)

@root(service)> list PersistenceProvider
[javax.persistence.spi.PersistenceProvider]
-------------------------------------------
 javax.persistence.provider = org.hibernate.jpa.HibernatePersistenceProvider
 service.id = 708
Provided by :
 hibernate-osgi (125)

持久性 XML

<?xml version="1.0" encoding="UTF-8" ?>
<persistence version="2.0"
    xsi:schemaLocation="
        http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <persistence-unit name="person" transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/MotorDB)</jta-data-source>
        <class>com.vogelware.simple.model.impl.jpa.Person</class>
        <class>com.vogelware.simple.model.impl.jpa.Address</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>

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

数据源蓝图

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    default-activation="eager">

    <bean id="MotorDB" class="org.apache.derby.jdbc.EmbeddedDataSource">
        <property name="databaseName" value="D:\dev\projects\EASA-DB\motordb" />
    </bean>

    <service ref="MotorDB" interface="javax.sql.DataSource">
        <service-properties>
            <entry key="osgi.jndi.service.name" value="jdbc/MotorDB" />
        </service-properties>
    </service>
</blueprint>

持久性 bean

<?xml version="1.0" encoding="UTF-8"?>
<blueprint default-activation="eager"
    xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jpa="http://aries.apache.org/xmlns/jpa/v1.0.0" xmlns:tx="http://aries.apache.org/xmlns/transactions/v1.0.0"
    xsi:schemaLocation="
            http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0 
            http://www.w3.org/2001/XMLSchema-instance http://www.w3.org/2001/XMLSchema-instance 
            http://aries.apache.org/xmlns/jpa/v1.0.0 http://aries.apache.org/xmlns/jpa/v1.0.0 
            http://aries.apache.org/xmlns/transactions/v1.0.0 http://aries.apache.org/xmlns/transactions/v1.0.0 ">

    <bean id="personService" class="com.vogelware.simple.remote.jpa.PersonEntityImplJpa">
        <jpa:context unitname="person" property="entityManager" />
        <tx:transaction method="*" value="Required" />
    </bean>

    <service ref="personService"
        interface="com.vogelware.simple.service.IPersonEntity">
        <service-properties>
            <entry key="osgi.jndi.service.name" value="hibernate/con_managed_personService" />
        </service-properties>
    </service>
</blueprint>

【问题讨论】:

我将persistence.xml 更改为具有匹配服务org.hibernate.jpa.HibernatePersistenceProvider 的PersistenceProvider,但仍然等待依赖项。 【参考方案1】:

您很可能遇到 aries jpa 中的错误。您可以尝试使用 karaf 3.0.2 吗?它包括较新的 aries jpa 功能。您还应该检查您是否只加载了 jpa 2.1 api。如果您加载了 jpa 2.0 和 2.1 api,它将无法工作。

【讨论】:

Christian ...感谢您周末的帮助!我尝试了 Karaf 3.02 以及企业功能 3.02-SNAPSHOT。两次尝试的结果相同。没有启动 EntityManagerService。我不确定如何检查 JPA 的女巫版本是否已加载。 不幸的是,karaf 3.0.2 也不包含最新版本。我们刚刚在 karaf 用户列表上收到了与您类似的请求。您现在可以尝试使用自定义功能。请参阅github.com/cschneider/Karaf-Tutorial/blob/master/tasklist-cdi/… 我将 karaf 配置为使用 hibernate 4.3 和最新的 aries 版本。有了这些,它工作得很好。对于下一个 karaf 版本,我们将确保标准功能已经包含该功能。 再次感谢您的教程!我发现了这个问题。我的持久性捆绑包中的 MANIFEST.MF 中缺少 Meta-Persistence: META-INF/persistence.xml 标头。 酷它现在可以工作了。缺少的 maven 蓝图插件现在也发布了。

以上是关于使用 Hibernate 的 Karaf 不会启动我的持久性单元的主要内容,如果未能解决你的问题,请参考以下文章

您可以在不使用 JTA 的情况下在 OSGi 环境(如 Karaf)中使用 Hibernate 5.2 StatelessSession 吗?

如何配置 Hibernate 以在 Karaf JPA 示例中工作?

启动捆绑包时缺少要求 osgi.wiring.package

在 karaf 中启动包时出错:“通过两个依赖链”

如何让 Spring JPA、Hibernate 和 OSGi 发挥出色?

Apache Karaf配置远程调试