Skip to main content

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

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:http://lsd.luminis.nl/jpa-persistence-in-osgi-with-openjpa/. 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 http://wiki.eclipse.org/EclipseLink/Examples/OSGi/Developing_with_EclipseLink_OSGi_in_PDE 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 :)

Teemu



Back to the top