Hi brmeyer,
I change to the
equinox 3.7 and I downloaded the
Hibernate 4.3 RC2 version. The problem is the same. But this time I was also debugging and when I step through the "persistenceBundle.adapt( BundleWiring.class )" call in OsgiArchiveDescriptor's constructor then I detected that the null object it is coming from
AbstractBundle class from the package
org.eclipse.osgi.framework.internal.coreThis class try to return the adapter to the bundleWiring object in the next method:
Code:
protected <A> A adapt0(Class<A> adapterType) {
if (adapterType.isInstance(this))
return (A) this;
if (BundleContext.class.equals(adapterType)) {
try {
return (A) getBundleContext();
} catch (SecurityException e) {
return null;
}
}
if (AccessControlContext.class.equals(adapterType)) {
return (A) getAccessControlContext();
}
if (BundleWiring.class.equals(adapterType)){
if (state == UNINSTALLED)
return null;
BundleDescription description = getBundleDescription();
return (A) description.getWiring();
}
if (BundleRevision.class.equals(adapterType)) {
if (state == UNINSTALLED)
return null;
return (A) getBundleDescription();
}
return null;
}
The adapterType argument is already the BundleWiring.class, but when the program go through the "
if (BundleWiring.class.equals(adapterType))" is not going into this if clause!!! Something strange.
My configuration is very simple but I want to tell you maybe I am committing a configuration mistake.
**************************************************************************
My first bundle: "testhibernate", just only have all the next jars from the Hibernate 4.3 RC2 version:
antlr-2.7.7
c3p0-0.9.2.1
dom4j-1.6.1
hibernate-c3p0-4.3.0.CR2
hibernate-commons-annotations-4.0.4.Final
hibernate-core-4.3.0.CR2
hibernate-entitymanager-4.3.0.CR2
hibernate-jpa-2.1-api-1.0.0.Final
hibernate-jpamodelgen-4.3.0.CR2
hibernate-osgi-4.3.0.CR2
jandex-1.1.0.Final
javassist-3.18.1-GA
jboss-logging-3.1.3.GA
jboss-logging-annotations-1.2.0.Beta1
jboss-transaction-api_1.2_spec-1.0.0.Final
mchange-commons-java-0.2.3.4
org.osgi.compendium-4.3.1
org.osgi.core-4.3.1This bundle export all the classes from the jar files and has the activator
org.hibernate.osgi.HibernateBundleActivator to register the service and the properties
Bundle-ActivationPolicy: lazy and
Eclipse-BuddyPolicy: registered****************************************************
The second bundle is my client bundle:
This import the
jdbc classes,
javassist.util.proxy,
javax.persistence,
javax.persistence.spi and
org.hibernate.proxy and of course
org.osgi.framework. Also required the first plugin.
In the Manifest.mf file I also added
Eclipse-RegisterBuddy: testhibernate ,
Meta-Persistence: META-INF/persistence.xml and
Bundle-ActivationPolicy: lazy.
My HibernateJpaActivator for this bundle just only want to get the entityManager:
Code:
public class HibernateJpaActivator implements BundleActivator {
private BundleContext context;
private HibernateUtil hb = new HibernateUtil();
public void start(BundleContext bundleContext) throws Exception {
this.context = bundleContext;
EntityManager em = hb.getEntityManager();
}
And my HibernateUtil class just only try to get the EntityManagerFactory using:
Bundle thisBundle = FrameworkUtil.getBundle( HibernateUtil.class );
BundleContext context = thisBundle.getBundleContext();
ServiceReference serviceReference = context.getServiceReference( PersistenceProvider.class.getName() );
PersistenceProvider persistenceProvider = (PersistenceProvider) context.getService( serviceReference );
emf = persistenceProvider.createEntityManagerFactory( "PersistenceProcessDataUnit", null );Before to call I have checked if the service is running from my first (testhibernate) bundle and it is working:
id State Bundle
0 STARTING org.eclipse.osgi_3.9.1.v20130814-1242
5 ACTIVE org.apache.felix.gogo.runtime_0.10.0.v201209301036
9 ACTIVE org.eclipse.equinox.console_1.0.100.v20130429-0953
14 ACTIVE org.apache.felix.gogo.command_0.10.0.v201209301215
17 ACTIVE org.apache.felix.gogo.shell_0.10.0.v201212101605
121 ACTIVE testhibernate_1.0.0
122 STARTING testhibernateclient_1.0.0.qualifierService:
{javax.persistence.spi.PersistenceProvider}={javax.persistence.provider=org.hibernate.jpa.HibernatePersistenceProvider, service.id=45}
"Registered by bundle:" testhibernate_1.0.0 [121]
"Bundles using service"
testhibernateclient_1.0.0.qualifier [122]So I don't know how can I use Hibernate in the right way in Equinox. I mean using the service and Unmanaged JPA, because if I try to persist using persistence class then I can persist.
Thanks
P.D. I feel happy to help you at least!!!
brmeyer wrote:
Hey, thanks for all the details -- helpful.
Out of curiousity, when you were debugging, did you step through the "persistenceBundle.adapt( BundleWiring.class )" call in OsgiArchiveDescriptor's constructor? I wonder if Equinox isn't handling #adapt correctly. Additionally, have tried this with any earlier versions of Equinox? I'd be most interested in 3.7 which, I think, was the last version on OSGi 4.3. 3.8+ is OSGi 5, and I wonder if that's screwing with something.
One other thing to be careful of is possibly "HibernateJpaActivator.getContext();". I'm assuming you got this from here: http://stackoverflow.com/questions/18832988/running-hibernate-in-an-eclipse-plugin-based-environment. Avoid static references like the plague in OSGi. Store #context in a non-static variable within HibernateJpaActivator, make #getContext non-static, then pass in the HibernateJpaActivator object to your dao/util (or wherever you're using the context to discover services).
You might also grab our 4.3.0.CR2 (released last week) -- numerous OSGi fixes in it.