-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Hibernate in an Osgi Equinox
PostPosted: Fri Dec 06, 2013 12:14 pm 
Newbie

Joined: Fri Dec 06, 2013 11:04 am
Posts: 6
Hi all,

I am trying to persist just a DataPoint Entity in my database in an OSGI Equinox and I am getting an null exception. (Hibernate 4.3.0 CR1 and Equinox 3.9 (Kepler SR1))

I am following the tutorial about client bundle in the Chapter 17. OSGi (devguide), but the examples are for Apache Karaf.

I read the official documentation (Chapter 17.8 devguide) the next:

Currently, Hibernate OSGi is primarily tested using Apache Karaf and Apache Aries JPA. Additional testing is needed with Equinox, Gemini, and other container providers.

So I decided to post here my problem. I am new, and i just want to say sorry if it is not the right place.

Ok.

I have only two bundles:

1.- Hibernate bundle (with all the jars, into the classpath and exporting all the packages, with the org.hibernate.osgi.HibernateBundleActivator as Activator for this bundle)

2.- Client Bundle (with my DataPoint entity, persistence.xml in the META-INF folder, and package required imported: javassist.util.proxy, javax.persistence, javax.persistence.spi, org.hibernate.proxy and so on)

In the activator of my client bundle i have the next code:

BundleContext context = HibernateJpaActivator.getContext();
ServiceReference serviceReference = context.getServiceReference( PersistenceProvider.class.getName() );
PersistenceProvider persistenceProvider = (PersistenceProvider) context.getService( serviceReference );
EntityManagerFactory emf = persistenceProvider.createEntityManagerFactory( "PersistenceProcessDataUnit", null );
EntityManager em = emf.createEntityManager();


em.getTransaction().begin();
DataPoint dt = new DataPoint();
dt.setName("new name");
em.persist(dt);
em.getTransaction().commit();
em.close();

I get the Service and the persistence provider but when I call to create the entityManagerFactory I get the next:

************************************************************************
************************************************************************

Dez 06, 2013 5:00:01 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
name: PersistenceProcessDataUnit
...]
Dez 06, 2013 5:00:01 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.0.CR1}
Dez 06, 2013 5:00:01 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Dez 06, 2013 5:00:01 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist

org.osgi.framework.BundleException: Exception in testhibernatedao.HibernateJpaActivator.start() of bundle testhibernateclient.
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:734)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:683)
at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:381)
at org.eclipse.osgi.framework.internal.core.AbstractBundle.resume(AbstractBundle.java:390)
at org.eclipse.osgi.framework.internal.core.Framework.resumeBundle(Framework.java:1176)
at org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:559)
at org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:544)
at org.eclipse.osgi.framework.internal.core.StartLevelManager.incFWSL(StartLevelManager.java:457)
at org.eclipse.osgi.framework.internal.core.StartLevelManager.doSetStartLevel(StartLevelManager.java:243)
at org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:438)
at org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:1)
at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)
Caused by: java.lang.NullPointerException
at org.hibernate.osgi.OsgiArchiveDescriptor.visitArchive(OsgiArchiveDescriptor.java:65)

at org.hibernate.jpa.boot.scan.spi.AbstractScannerImpl.scan(AbstractScannerImpl.java:72)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.scan(EntityManagerFactoryBuilderImpl.java:727)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:219)
at org.hibernate.jpa.boot.spi.Bootstrap.getEntityManagerFactoryBuilder(Bootstrap.java:49)
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:130)
at org.hibernate.osgi.OsgiPersistenceProvider.createEntityManagerFactory(OsgiPersistenceProvider.java:96)
at testhibernatedao.HibernateUtil.getEntityManagerFactory(HibernateUtil.java:24)
at testhibernatedao.HibernateUtil.getEntityManager(HibernateUtil.java:15)
at testhibernatedao.HibernateJpaActivator.start(HibernateJpaActivator.java:24)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(BundleContextImpl.java:711)
at java.security.AccessController.doPrivileged(Native Method)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:702)
... 12 more
Root exception:
java.lang.NullPointerException
at org.hibernate.osgi.OsgiArchiveDescriptor.visitArchive(OsgiArchiveDescriptor.java:65)

at org.hibernate.jpa.boot.scan.spi.AbstractScannerImpl.scan(AbstractScannerImpl.java:72)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.scan(EntityManagerFactoryBuilderImpl.java:727)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:219)
at org.hibernate.jpa.boot.spi.Bootstrap.getEntityManagerFactoryBuilder(Bootstrap.java:49)
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:130)
at org.hibernate.osgi.OsgiPersistenceProvider.createEntityManagerFactory(OsgiPersistenceProvider.java:96)
at testhibernatedao.HibernateUtil.getEntityManagerFactory(HibernateUtil.java:24)
at testhibernatedao.HibernateUtil.getEntityManager(HibernateUtil.java:15)
at testhibernatedao.HibernateJpaActivator.start(HibernateJpaActivator.java:24)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(BundleContextImpl.java:711)
at java.security.AccessController.doPrivileged(Native Method)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:702)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:683)
at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:381)
at org.eclipse.osgi.framework.internal.core.AbstractBundle.resume(AbstractBundle.java:390)
at org.eclipse.osgi.framework.internal.core.Framework.resumeBundle(Framework.java:1176)
at org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:559)
at org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:544)
at org.eclipse.osgi.framework.internal.core.StartLevelManager.incFWSL(StartLevelManager.java:457)
at org.eclipse.osgi.framework.internal.core.StartLevelManager.doSetStartLevel(StartLevelManager.java:243)
at org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:438)
at org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:1)
at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)



**********************************************************************************************
**********************************************************************************************

I was debugging and all is ok until the code try to call => final Collection<String> resources = bundleWiring.listResources( "/", "*", BundleWiring.LISTRESOURCES_RECURSE );

in the OsgiArchiveDescriptor class because bundleWiring object is null

All the properties from my persistence.xml are reading right and also and if instead of using the service, I just try to persist using the not Osgi way:

Persistence.createEntityManagerFactory( "PersistenceProcessDataUnit", null );

Then i just persist the entity.

Thanks


Top
 Profile  
 
 Post subject: Re: Hibernate in an Osgi Equinox
PostPosted: Mon Dec 09, 2013 6:17 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 18, 2012 5:03 am
Posts: 36
Location: Fort Wayne, Indiana, USA
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.


Top
 Profile  
 
 Post subject: Re: Hibernate in an Osgi Equinox
PostPosted: Tue Dec 10, 2013 5:12 am 
Newbie

Joined: Fri Dec 06, 2013 11:04 am
Posts: 6
Hey,

I was try also with this example as you were assuming right (http://stackoverflow.com/questions/18832988/running-hibernate-in-an-eclipse-plugin-based-environment), so I change the code in order to get the context in my HibernateUtil class like the next code:

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 );



Also as you have supposed I am using Kepler that implement 3.8+ equinox so Osgi 5.

When I am debugging, then I step through the persistenceBundle.adapt( BundleWiring.class ) in the OsgiArchiveDescriptor's constructor in fact and is when a I get the null object for this bundleWiring object.

Before to continue I am going to change to equinox 3.7 and then I am going to give you a feedback.

Thanks


Top
 Profile  
 
 Post subject: Re: Hibernate in an Osgi Equinox
PostPosted: Tue Dec 10, 2013 9:36 am 
Newbie

Joined: Fri Dec 06, 2013 11:04 am
Posts: 6
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.core

This 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.1


This 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.qualifier


Service:

{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.


Top
 Profile  
 
 Post subject: Re: Hibernate in an Osgi Equinox
PostPosted: Wed Dec 11, 2013 8:05 am 
Newbie

Joined: Fri Dec 06, 2013 11:04 am
Posts: 6
Ok, now is solved!!! I am going to explain how I connected Hibernate with OSGI.

First at all I updated the last version of Hibernate to hibernate-release-4.3.0.CR2 like brmeyer suggest me. Then I have created a folder with all the libraries that I need from hibernate, in my case:

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.1


But also you need to add the next libraries because they are not coming with Hibernate distribution but the bundles need it:

cdi-api-1.1-20130918
classmate-0.8.0
javax.interceptor-api-1.2


Then you have to include all these libraries in your eclipse target platform. Go through the Windows -> Preferences -> Plug-in Development -> Target Platform, select the running platform click on edit button, and then under locations add the directory with all the libraries. Then in content tab select all and finish. Maybe you need to restart eclipse.

After that, then you can create your hibernate bundle (testhibernate in my case) with all the libraries also in the classpath and exporting all the packages.

Then in the client bundle you can follow the tutorial from the official documentation http://docs.jboss.org/hibernate/orm/4.3/devguide/en-US/html_single/#d5e4803 . Unmanaged JPA in my case.

After this, when you are going to run, you must to configure the next running configuration (the first 2 bundles are my hibernates bundle):

testhibernate
testhibernateclient
classmate 0.8.0
javassist 3.18.1.GA
javax.el 2.2.0.v201303151357
javax.enterprise.cdi-api 1.1.0.20130918
javax.inject 1.0.0.v20091030
javax.interceptor-api 1.2.0
javax.servlet 3.0.0.v201112011016
javax.xml 1.3.4.v201005080400
org.apache.felix.gogo.command 0.10.0
org.apache.felix.gogo.runtime 0.10.0
org.apache.felix.shell 0.10.0
org.eclipse.core.contenttype 3.4.200
org.eclipse.core.jobs 3.5.300
org.eclipse.core.runtime 3.9.0 Start Level: 2 Auto-Start: true
org.eclipse.equinox.app 1.3.100
org.eclipse.equinox.common 3.6.200 Start Level: 2 Auto-Start: true
org.eclipse.equinox.console 1.1.100
org.eclipse.equinox.launcher 1.3.0
org.eclipse.equinox.preferences 3.5.100
org.eclipse.equinox.registry 3.5.301
org.eclipse.osgi 3.9.1 Start Level: -1 Auto-Start: true
org.eclipse.osgi.services 3.3.100
org.eclipse.osgi.util 3.2.300
org.hibernate.c3p0 4.3.0 CR2
org.hibernate.common.hibernate-commons-annotations 4.0.4 Final
org.hibernate.core 4.3.0 CR2
org.hibernate.entitymanager 4.3.0.CR2
org.hibernate.javax.persistence.hibernate-jpa-2.1-api 1.0.0 Final
org.hibernate.jpamodelgen 4.3.0 CR2
org.hibernate.osgi 4.3.0.CR2
org.jboss.logging.jboss-logging 3.1.3.GA
org.jboss.spec.javax.transaction.jboss-transaction-api_1.2_spec 1.0.0.Final



At least this work for me and I can persist the entities in my testhibernateclient bundle.

I hope this can help people!!

Thanks brmeyer.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.