Hibernate.orgCommunity Documentation

Chapter 6. OSGi Tutorial

Table of Contents

6.1. Project Overview
6.2. Project Structure
6.3. TODOs
6.4. Karaf Commands

Hibernate targets the OSGi 4.3 spec or later and supports three types of configurations.

  1. Container-Managed JPA
  2. Unmanaged JPA
  3. Unmanaged Native

For more details about OSGi, the three configurations, hibernate-osgi, extensions points, and caveats, please see the OSGi chapter of the Developer's Guide!

Each configuration has a QuickStart project located within the download bundle (under osgi). The bundles can be used as-is within Apache Karaf. Feel free to use them as literal "quick start" bundle templates.

  • osgi/datasource-h2.xml: Enterprise OSGi JPA usage can include a DataSource installed in the container. The client bundle's persistence.xml references the DataSource through JNDI. For an example, see how managed-jpa's persistence.xml calls out the jta-data-source.
  • osgi/[project]/features.xml: This is arguably the most important "quick start" material. It defines a single Karaf feature ("hibernate-test") that demonstrates the necessary 3rd party libraries and bundle activation ordering.
  • osgi/[project]/pom.xml: The POM includes typical compile-time dependencies (JPA, OSGi Core, OSGi Enterprise), as well as OSGi manifest data.
  • osgi/[project]/src/main/resources/OSGI-INF/blueprint/blueprint.xml: The Blueprint includes container-managed EntityManager injection (for managed-jpa), as well as demonstrations showing how to register your custom implementations of Hibernate extension points.
  • osgi/[project]/src/main/resources/META-INF/persistence.xml or osgi/[project]/src/main/resources/hibernate.cfg.xml: Note that the configurations are no different than typical uses of Hibernate!
  • osgi/[project]/src/main/java/org/hibernate/osgitest/HibernateUtil.java: Demonstrates how to create an EntityManagerFactory (JPA) or SessionFactory (Native) using hibernate-osgi's services. Note that in managed-jpa, this is replaced by DataPointServiceImpl#entityManager, injected by blueprint.xml (described above).
  • If using managed-jpa, features.xml will need the path to datasource-h2.xml updated.

All three bundles include Karaf Commands that can be used directly on the Karaf command line to test basic persistence operations ("dp:add [name]", "dp:getall", "dp:deleteall", etc.). I leave them in the QuickStarts as a useful sanity check.