users@glassfish.java.net

RE: JPA/OSGi Bundle

From: Robert Weeks <RWeeks_at_ext-inc.com>
Date: Tue, 7 Jun 2011 21:01:37 -0700

Hello Sahoo -

Thanks for this information. Sorry about the stack trace - this was at the core not able to locate the persistence information from within a bundle (but worked fine from standalone app).

Is the process below documented anywhere? We looked for info for a bit on this - and could not resolve easily.

I will try this process tomorrow. Thanks again for the info!



Robert Weeks
Lead Developer - Framework and UI
EXTENSION, INC.
Email: rweeks_at_ext-inc.com
Office: 260-797-0200 x4228
General: 877-207-3753
www.OpenTheRedBox.com

Download the FREE EXTENSION Mobile App for iPhone and iPod Touch
http://www.opentheredbox.com/iPhone_appDemo.php

________________________________________
From: Sahoo [sanjeeb.sahoo_at_oracle.com]
Sent: Tuesday, June 07, 2011 10:57 PM
To: users_at_glassfish.java.net; Robert Weeks
Subject: Re: JPA/OSGi Bundle

Robert,

Are you using Persistence.createEMF()? What exception are you seeing? Here is what one has to do:

The Java SE bootstrapping API (Persistence.createEntityManagerFactory) relies on Thread's context class loader to be set properly for two reasons:
         to discover persistence units and JPA domain classes,
     •
         to discover Persistence Provider.
     •
While it is easy to work around the first one by just setting the context class loader to the bundle
class loader before making the call to the Persistence.createEntityManagerFactory,
the latter is hard to fix, as the bundle does not typically have a direct dependency on Persistence
Provider. Fortunately, GlassFish Server can influence the resolution of persistence providers using
a custom implementation of JPA SPI called javax.persistence.spi.PersistenceProviderResolver.
This custom resolver can be enabled by setting the following property to true in
$GLASSFISH_HOME/glassfish/osgi/felix/conf/config.properties:

org.glassfish.osgijpa.extension.useHybridPersistenceProviderResolver=true

It is by default set to false in that file.

So, you have to set the context class loader temporarily. You can look at our sample [1].

Thanks,
Sahoo

[1] http://java.net/projects/glassfish/sources/svn/content/trunk/fighterfish/sample/sample.uas/sample.uas.entities/src/main/java/org/glassfish/fighterfish/sample/uas/entities/EntitiesActivator.java?rev=47359

nb: Next time, please include exception stack trace.

On Wednesday 08 June 2011 05:48 AM, Robert Weeks wrote:

Hello -

Is there any special tricks to getting the persistence.xml file to be read correctly from within a bundle? We seem to not be able to get it to recognize at all - no matter where we put it (inside/outside META-INF), setting Bundle-ClassPath, etc.

Thanks for any information.