I have developed a small sample to demonstrate how to use Hibernate in OSGi with Spring Framework and Spring-DM. The sample works in either Felix or Equinox. It builds with Maven 2 – the build process automatically downloads the Felix and Equinox jars so there is no need to install either framework separately.
Starting Hibernate in OSGi with the normal Hibernate SessionFactory would allow you to use Hibernate in OSGi – but that would not address the dynamic behavior that an OSGi system is capable of. This sample demonstrates how a Hibernate SessionFactory can be updated dynamically in an OSGi environment. You can dynamically add and remove entity classes to the Hibernate configuration by stopping and starting bundles that contribute clasess to the Hibernate configuration.
This implementation contains a small Swing UI which allows you to see which entity classes are present in the Hibernate configuration. The Swing UI also allows you to issue rudimentary SQL and HQL queries.
The solution is based on this OSGi and Hibernate blog entry by Peter Kriens. The design uses an entry in the manifest file to declaratively add classes to a Hibernate session using an extender model.
Get the code
To get the source code for the hibernate sample, use SVN:
svn checkout http://voluble.googlecode.com/svn/trunk/osgi-samples/hibernate/ osgi-hibernate-sample
Run the code
Run “mvn install” in the new osgi-hibernate-sample folder and see all projects build successfully.
The 2nd last project that builds is “integration-tests”, which uses Spring DM to run an integration test suite in an OSGi container. The integration tests are run against an in-process HSQLDB server.