在 Jboss7 OSGI 上休眠(无 EJB)

Hibernate on Jboss7 OSGI (No EJB)

我使用实体 bean 和一些提供我的 HomeLocalHomeRemote 接口的无状态 ejb,我在其中注入 persistenceContext 并获得 EntityManager.

作为新要求(在 Karaf 上迁移)我必须摆脱所有 EJB。

我的问题是如何用简单的 DAO 类 替换这个无状态的 ejb 并在这些 类 中注入或获取实体管理器?

我的 JPA 提供程序处于休眠状态。

我需要一些示例、教程或任何类型的帮助。

您可以使用 Apache Aries 项目:

有趣的是你将使用蓝图,声明你的 bean 并定义一个服务(假设你想使用服务)

<blueprint 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">

    <bean id="jpaDemo" init-method="init" class="org.demo.osgi.datasource.jpa.JpaComponentImpl">
        <jpa:context unitname="demo" property="entityManager"/>
        <tx:transaction method="*" value="Required"/>
    </bean>

    <service ref="jpaDemo" interface="org.demo.osgi.datasource.jpa.JpaComponent"/>

</blueprint>

然后 JpaComponent 可以使用注入的 entityManager(Scala 中的代码,但我相信您会明白的)

trait JpaComponent {

}
class JpaComponentImpl extends JpaComponent {

  val logger = org.slf4j.LoggerFactory.getLogger(classOf[JpaComponent])

  @BeanProperty
  var entityManager : EntityManager = _

  def init = {
    logger.info(s"em=${entityManager}")
  }
}

persistence.xml 放入您的捆绑包中(例如 META-INF/persistence.xml)。以下示例:

<persistence-unit name="demo" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/jbtravel)</jta-data-source>
    <mapping-file>META-INF/airport.xml</mapping-file>
</persistence-unit>

您将需要以下功能:

  • jpa
  • 休眠
  • jndi
  • 交易

以及以下捆绑包

  • mvn:org.apache.aries/org.apache.aries.util/1.0.1
  • mvn:org.apache.aries.jpa/org.apache.aries.jpa.api/1.0.1
  • mvn:org.apache.aries.jpa/org.apache.aries.jpa.container.context/1.0.1

加上设置以下 OSGI 元数据

  • 元持久性:META-INF/persistence.xml
  • 服务组件:*

另请参阅 https://github.com/rparree/osgi-demos/tree/master/datasource 上面的示例