简体   繁体   English

在Jboss7 OSGI上休眠(无EJB)

[英]Hibernate on Jboss7 OSGI (No EJB)

I use entity beans and some Stateless ejb that provide my HomeLocal and HomeRemote interface, where I inject persistenceContext and obtain EntityManager . 我使用实体bean和一些提供HomeLocalHomeRemote接口的无状态ejb,在其中注入persistenceContext并获取EntityManager

As new requirement (migration on Karaf) I have to get rid of all EJB. 作为新要求(在Karaf上进行迁移),我必须摆脱所有EJB。

My question is how can I replace this stateless ejb with simple DAO classes and inject or obtain Entity manager in these classes? 我的问题是如何用简单的DAO类替换此无状态ejb,并在这些类中注入或获取实体管理器?

My JPA provider is hibernate. 我的JPA提供程序处于休眠状态。

I need some example, tutorials or any kind of help. 我需要一些示例,教程或任何形式的帮助。

You could use the Apache Aries project: 您可以使用Apache Aries项目:

Amusing you will be using blueprint, declare your bean and define a service (assuming you want to use services) 有趣的是您将使用蓝图,声明您的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>

The JpaComponent can then use the injected entityManager (code in Scala, but i'm sure you'll get the idea) 然后, 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}")
  }
}

Place a persistence.xml in your bundle (eg, META-INF/persistence.xml ). persistence.xml放在您的捆绑包中(例如META-INF/persistence.xml )。 Sample below: 示例如下:

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

You will need the following features: 您将需要以下功能:

  • jpa pa
  • hibernate 冬眠的
  • jndi 金迪
  • transaction 交易

And the following bundles 和以下捆绑

  • 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

Plus set the following OSGI meta-data 另外设置以下OSGI元数据

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

See also https://github.com/rparree/osgi-demos/tree/master/datasource for the sample from above 另请参阅https://github.com/rparree/osgi-demos/tree/master/datasource以获取上述示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM