This repo shows how to use JPA in an OSGi environment and with Eclipse 4.
It provides products for both EclipseLink and Hibernate.
cd com.vogella.jpa
./mvnw clean verify
After the successful build you can navigate to either the /home/simon/git/com.vogella.jpa/com.vogella.jpa.product.eclipselink/target/products/vogella-jpa-eclipselink/your OS/your ws/your arch/vogella-jpa-eclipselink or /com.vogella.jpa/com.vogella.jpa.product.hibernate/target/products/vogella-jpa-hibernate/your OS/your ws/your arch/vogella-jpa-hibernate and run the vogella-jpa-eclipselink or vogella-jpa-hibernate runnable.
I do not want to compare these two JPA implementations, but just mention that it is easier to include EclipseLink into a project, which uses an OSGi runtime, e.g., Eclipse, because for Hibernate it was still necessary to add the Eclipse-BuddyPolicy: registered
instruction in the models MANIFEST.MF file and to register the com.vogella.jpa.service.eclipselink project as buddy (Eclipse-RegisterBuddy: com.vogella.jpa.model
).
Tip
|
See https://wiki.eclipse.org/Context_Class_Loader_Enhancements for further information. |
Another point is that EclipseLink provides their OSGi bundles in a p2 update site (http://download.eclipse.org/rt/eclipselink/updates/) and Hibernate had to be added to the dependencies project, which generates a p2 update site. See dependencies for further information.
Building the project with Maven Tycho is nice, but as a developer you might want to see the code und run it from the Eclipse IDE. Therefore just clone and import the project, set the target platform, which is specified in the com.vogella.jpa.target project, and run the com.vogella.jpa.eclipselink.product product or the com.vogella.jpa.hibernate.product product.
The dependencies projects uses bnd-platform in order to generate a p2 update site for the H2 database and the Hibernate hibernate dependencies. See build.gradle and bnd-platforms documentation to see how this is done.
But no worries the p2 update site, which is generated is already available here: https://dl.bintray.com/simon-scholz/H2-Database-Engine/ and also being used in the target platform definition file.
This project consists of a com.vogella.jpa.target.target target definition file, which should be set as target platform for the workspace so that all projects compile.
This is a simple data model for the clients containing a Todo
entity and an TodoService
interface, but completely decoupled from EclipseLink or Hibernate. So it is pretty much like a DAO or a Spring data repository.
Implement the TodoService
interface and registers this implementation as an OSGi service so that it can the obtained by clients.
Depending on which product is started com.vogella.jpa.service.eclipselink or com.vogella.jpa.service.hibernate will be part of the product and therefore provide the actual implementation of the TodoService
.
This client is an Eclipse 4 RCP application, which only has a dependency to the com.vogella.jpa.model package and simply injects the TodoService
into a sample part in order to show Todo
objects in a sample part.
Please feel free to add new features like deleting or adding Todo
objects, since the TodoService
service already offers these methods as well.