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 http://wiki.eclipse.org/EclipseLink/Examples/OSGi/Equinox_Byte_Code_Weaving:
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,
Shaun
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.
http://www.eclipse.org/gemini/
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.
-Tom
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: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
------------------------------------------------------------------------
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users
--
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 is committed to developing practices and
products that help protect the environment
|