Monica
Monica

Reputation: 188

JPA EntityManager discover entities from other bundle (OSGI)

In an OSGI environment such as Equinox, i want to have 2 bundles (or more bundles) and each one has his own JPA annoted entities (classes). I want to have a dependency between the classes from each bundle.

For example: I have bundle 1 with the class Person and bundle 2 with the class Employee which extends Person (this is most simple example i can think of - i know i could declare the both classes in one bundle but i need them to be in separate bundles).

Is it possible to expose an EntityManager or EntityManagerFactory as an OSGI service which knows about the entities present in both bundles? I know that each bundle can have his own EntityManager(EM) but in the given example the EM in bundle 2 doesn't know about the entity Person. If is possible can anyone give an example or sugest how to do this?

Thanks for your help!

Upvotes: 1

Views: 686

Answers (2)

Christian Schneider
Christian Schneider

Reputation: 19606

Why should this not work? If Employee extends Person then bundle 2 will import the package that contains Person. So it has a dependency to bundle1 1. In the persistence.xml of bundle 2 you should be able to list both Employee and Person. So both work in the Entitymanager of bundle 2. Of course the EntityManager of bundle 1 can not know about Employee.

Upvotes: 0

Tassos Bassoukos
Tassos Bassoukos

Reputation: 16142

I want to have a dependency between the classes from each bundle.

This is not possible when using Equinox + OSGI. The nearest you can achieve using unmodified Equinox and Gemini JPA is using composite persistence units, but that still requires that your entities are in the same bundle but in separate JARs.

I've created a hack using a patched Gemini JPA bundle that can dynamically create a composite peristence context by merging persistence contexts across bundles, but it's extremely hairy and finicky; it has dependencies on bundle loading order and it will not react properly to bundle reloads, hence mooting half the purpose of OSGi...

OTOH, you can expose separate entity managers and use them both in both bundles as long as you accept that there will be a short period of time where only one of them will be available. Nothing prevents you in Bundle B tracking the EntityManager of Bundle A (well, except cyclic bundle dependencies, but that happens oly if you cross-import packages).

Upvotes: 1

Related Questions