Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] JPA, OSGI, Eclipselink, class loaders

Hello Shaun & Tom,

 Thanks for your replies. I noted Gemini in my search but did not pursue that further at that time. This is because JPA is just one technology for me in building my software and I would hope that it would not force me into a specific container implementation. I think that's what a "standard" like JPA should enable. However, I will try to keep an eye on this stuff since it is interesting.

 I am curious about the OSGI Java EE specifications you mention. Can you point me to some links for further information on what they are?

 Is the JPA-PersistenceUnit header specific to EclipseLink? And is the classloader property also EclipseLink specific? That is are they supported in general in OSGI containers/JPA providers or just EclipseLink? I never noticed those before,  thanks for clearing that up. At least now I know how to make one JPA provider work in a more "official way" if my current hack fails :) Maybe this stuff could find a more explicit mention in the docs as well.


2011/2/3 Shaun Smith <shaun.smith@xxxxxxxxxx>
Hi Teemu,

As Tom points out Gemini JPA will deliver OSGi compliant JPA and that is where the future lies.

However the current EclipseLink support for JPA in OSGi is not all that different from the OSGi spec and was certainly drawn upon when the spec was being authored.  As you suggest, there are lots of "tricks behind the scenes" to make JPA work in OSGi and EclipseLink is doing them. :-)  If you follow the instructions on the examples you'll see what you have to do--which isn't all that much.  The key thing is declaring the JPA-PersistenceUnits header in your bundle containing the persistence.xml.  EclipseLink locates persistence units (declared in persistence.xml) in bundles using this header.   In the OSGi spec this header is "Meta-Persistence" and is used slightly differently but the intent is the same.

Also, you can indeed pass a classloader to EntityManagerFactory.createEntityManagerFactory and this classloader will be used to resolve classes and metadata but the persistence.xml *must* be declared using the JPA-PersistenceUnit header.  From
import org.eclipse.persistence.config.PersistenceUnitProperties;
Map<String,Object> properties = new HashMap<String, Object>();
properties.put(PersistenceUnitProperties.CLASSLOADER, this.getClass().getClassLoader());
emf = Persistence.createEntityManagerFactory("comics", properties);
This example also demonstrates how to run outside Eclipse PDE.

Hope this helps, 


On 03/02/2011 9:10 AM, Tom Ware wrote:
Hi Teemu,

  Have you had a look at the Eclipse Gemini project?  It will be the Reference Implementation for the OSGi Java EE specifications.

  There is a sub-project for JPA support.  The documentation is sparse at the moment, but the mailing lists and newsgroups have pretty good response are are likely the best way to get advice about how to get started.

  Since this project is based on the actual specification, I suspect that JPA providers that want to provide OSGi services will migrate to work based on Gemini.


teemu kanstren wrote:
Dear all,

 I have been trying to get my OSGI stuff to work with using JPA. As I am sure many here know, I need to create the EntityManagerFactory with  Persistence.createEntityManagerFactory("my-factory"). Now, behind the scenes (am I supposed to read the sourcecode of the libraries as well? :)) this loads the persistence.xml from the META-INF directory on the classpath. With OSGI this always gives me the error "javax.persistence.PersistenceException: No Persistence provider for EntityManager named my-factory". After plenty of googling I came up with the reason that the persistence.xml is loaded using ThreadContext classloader, which in OSGI naturally fails. Reading up on it I also found only suggestions to use EclipseLink since it is "the only one build for OSGI and the JPA RI". Yet I get the same errors with it as well.

 The only solution (after looong debugging sessions) I found that would work is the one given here: That is, to set the threadcontext classloader before call to createEntityManagerFactory and reset it after. I use Apache Felix as the OSGI container and I tried with both Hibernate JPA and EclipseLink JPA. Same problem every time, yet outside OSGI it all works perfectly (running the same code with JUnit). So, given that EclipseLink is the JPA RI and targets an OSGI environment, maybe someone can give me some answers here. My questions

- Why is there no option in Persistence.createEntityManagerFactory() to provide your own classloader to be used in loading of resources?

- Why is the only error message you can get for this the "javax.persistence.PersistenceException: No Persistence provider for EntityManager named my-factory"? No mention of "unable to find persistence.xml in classpath which is..." or "using classloaders..." or whatever. Just "unable to find" is a pretty bad message since it should know quite a bit more about why it is not able to find it (like missing persistence.xml for some reason).

Using the trick in the link above it all works fine. The only difference with "for OSGI" in EclipseLink vs Hibernate I see is that I need to import/export some stuff myself. Not much of an achievement considering how cryptic it is to get it working at all. Am I missing something?

Maybe there is some magic trick I miss since on the EclipseLink documentation such as it all seems to be oh so easy. Or maybe Eclipse does some tricks behind the scenes? Anyway, I was left thinking you can only run it inside Eclipse as this is what is always used in the docs.

To end more positively, thanks for all the efforts on JPA. Once I learn it, and when it works, it is nice enough :)



eclipselink-users mailing list
eclipselink-users mailing list

Shaun Smith | Principal Product Manager
Phone: +1.905.502.3094 | Mobile: +1.416.558.6244
Oracle Fusion Middleware, TopLink
ORACLE Canada | 110 Matheson Boulevard West, Suite 100, Mississauga, Ontario | L5R 3P4

          Oracle Oracle is committed to developing practices and products that help protect the environment

eclipselink-users mailing list

Back to the top