9 Replies Latest reply on Sep 3, 2009 9:28 PM by oren

    ClassNotFoundException in org.hibernate.annotations.commons.reflection

    oren

      I have a bundle that depends on Hibernate, and I just can't get it to see the Hibernate annotations common bundle that is installed on the bus. I keep getting

       

      smx@root:osgi> Exception in thread "SpringOsgiExtenderThread-112" org.springfram

      ework.beans.factory.UnsatisfiedDependencyException: Error creating bean with nam

      e 'dao' defined in URL [bundle://211.0:1/edu/utah/further/core/data/service/DaoH

      ibernateImpl.class]: Unsatisfied dependency expressed through constructor argume

      nt with index 0 of type : : Error creating bean wi

      th name 'sessionFactory' defined in URL [bundle://225.0:0/META-INF/spring/mdr-im

      pl-context-datasource.xml]: Invocation of init method failed; nested exception i

      s java.lang.NoClassDefFoundError: org/hibernate/annotations/common/reflection/Re

      flectionManager; nested exception is org.springframework.beans.factory.BeanCreat

      ionException: Error creating bean with name 'sessionFactory' defined in URL [bun

      dle://225.0:0/META-INF/spring/mdr-impl-context-datasource.xml]: Invocation of in

      it method failed; nested exception is java.lang.NoClassDefFoundError: org/hibern

      ate/annotations/common/reflection/ReflectionManager

              at org.springframework.beans.factory.support.ConstructorResolver.createA

      rgumentArray(ConstructorResolver.java:591)

              at org.springframework.beans.factory.support.ConstructorResolver.autowir

      eConstructor(ConstructorResolver.java:193)

              at org.springframework.beans.factory.support.AbstractAutowireCapableBean

      Factory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:925)

              at org.springframework.beans.factory.support.AbstractAutowireCapableBean

      Factory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:835)

              at org.springframework.beans.factory.support.AbstractAutowireCapableBean

      Factory.doCreateBean(AbstractAutowireCapableBeanFactory.java:440)

              at org.springframework.beans.factory.support.AbstractAutowireCapableBean

      Factory$1.run(AbstractAutowireCapableBeanFactory.java:409)

              at java.security.AccessController.doPrivileged(Native Method)

              at org.springframework.beans.factory.support.AbstractAutowireCapableBean

      Factory.createBean(AbstractAutowireCapableBeanFactory.java:380)

              at org.springframework.beans.factory.support.AbstractBeanFactory$1.getOb

      ject(AbstractBeanFactory.java:264)

              at org.springframework.beans.factory.support.DefaultSingletonBeanRegistr

      y.getSingleton(DefaultSingletonBeanRegistry.java:222)

              at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBe

      an(AbstractBeanFactory.java:261)

              at org.springframework.beans.factory.support.AbstractBeanFactory.getBean

      (AbstractBeanFactory.java:185)

              at org.springframework.beans.factory.support.AbstractBeanFactory.getBean

      (AbstractBeanFactory.java:164)

              at org.springframework.beans.factory.support.DefaultListableBeanFactory.

      preInstantiateSingletons(DefaultListableBeanFactory.java:429)

              at org.springframework.context.support.AbstractApplicationContext.finish

      BeanFactoryInitialization(AbstractApplicationContext.java:728)

              at org.springframework.osgi.context.support.AbstractDelegatedExecutionAp

      plicationContext.access$1600(AbstractDelegatedExecutionApplicationContext.java:6

      8)

              at org.springframework.osgi.context.support.AbstractDelegatedExecutionAp

      plicationContext$4.run(AbstractDelegatedExecutionApplicationContext.java:343)

              at org.springframework.osgi.util.internal.PrivilegedUtils.executeWithCus

      tomTCCL(PrivilegedUtils.java:85)

              at org.springframework.osgi.context.support.AbstractDelegatedExecutionAp

      plicationContext.completeRefresh(AbstractDelegatedExecutionApplicationContext.ja

      va:308)

              at org.springframework.osgi.extender.internal.dependencies.startup.Depen

      dencyWaiterApplicationContextExecutor$CompleteRefreshTask.run(DependencyWaiterAp

      plicationContextExecutor.java:138)

              at java.lang.Thread.run(Thread.java:619)

      Caused by: org.springframework.beans.factory.BeanCreationException: Error creati

      ng bean with name 'sessionFactory' defined in URL [bundle://225.0:0/META-INF/spr

      ing/mdr-impl-context-datasource.xml]: Invocation of init method failed; nested e

      xception is java.lang.NoClassDefFoundError: org/hibernate/annotations/common/ref

      lection/ReflectionManager

              at org.springframework.beans.factory.support.AbstractAutowireCapableBean

      Factory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)

              at org.springframework.beans.factory.support.AbstractAutowireCapableBean

      Factory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)

              at org.springframework.beans.factory.support.AbstractAutowireCapableBean

      Factory$1.run(AbstractAutowireCapableBeanFactory.java:409)

              at java.security.AccessController.doPrivileged(Native Method)

              at org.springframework.beans.factory.support.AbstractAutowireCapableBean

      Factory.createBean(AbstractAutowireCapableBeanFactory.java:380)

              at org.springframework.beans.factory.support.AbstractBeanFactory$1.getOb

      ject(AbstractBeanFactory.java:264)

              at org.springframework.beans.factory.support.DefaultSingletonBeanRegistr

      y.getSingleton(DefaultSingletonBeanRegistry.java:222)

              at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBe

      an(AbstractBeanFactory.java:261)

              at org.springframework.beans.factory.support.AbstractBeanFactory.getBean

      (AbstractBeanFactory.java:185)

              at org.springframework.beans.factory.support.AbstractBeanFactory.getBean

      (AbstractBeanFactory.java:164)

              at org.springframework.beans.factory.support.DefaultListableBeanFactory.

      findAutowireCandidates(DefaultListableBeanFactory.java:671)

              at org.springframework.beans.factory.support.DefaultListableBeanFactory.

      resolveDependency(DefaultListableBeanFactory.java:610)

              at org.springframework.beans.factory.support.ConstructorResolver.resolve

      AutowiredArgument(ConstructorResolver.java:622)

              at org.springframework.beans.factory.support.ConstructorResolver.createA

      rgumentArray(ConstructorResolver.java:584)

              ... 20 more

      Caused by: java.lang.NoClassDefFoundError: org/hibernate/annotations/common/refl

      ection/ReflectionManager

              at java.lang.Class.getDeclaredConstructors0(Native Method)

              at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)

              at java.lang.Class.getConstructor0(Class.java:2699)

              at java.lang.Class.getDeclaredConstructor(Class.java:1985)

              at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:7

      8)

              at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newConfigu

      ration(LocalSessionFactoryBean.java:772)

              at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessi

      onFactory(LocalSessionFactoryBean.java:517)

              at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPr

      opertiesSet(AbstractSessionFactoryBean.java:211)

              at org.springframework.beans.factory.support.AbstractAutowireCapableBean

      Factory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)

              at org.springframework.beans.factory.support.AbstractAutowireCapableBean

      Factory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)

              ... 33 more

      Caused by: java.lang.ClassNotFoundException: org.hibernate.annotations.common.re

      flection.ReflectionManager

              at org.apache.felix.framework.searchpolicy.ModuleImpl.findClassOrResourc

      eByDelegation(ModuleImpl.java:558)

              at org.apache.felix.framework.searchpolicy.ModuleImpl.access$100(ModuleI

      mpl.java:59)

              at org.apache.felix.framework.searchpolicy.ModuleImpl$ModuleClassLoader.

      loadClass(ModuleImpl.java:1427)

              at java.lang.ClassLoader.loadClass(ClassLoader.java:252)

              at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)

              ... 43 more

       

      smx@root:osgi>

      smx@root:osgi>

      smx@root:osgi>

       

      ... even though it is declared in the felix plugin config of my bundle (see attached pom), and the dependency exists on the ESB:

       

      list | grep Hibernate

      JBoss Hibernate Annotations (3.4.0.GA)

      JBoss Hibernate Entity Manager (3.4.0.GA)

       

      JBoss Hibernate Object-Relational Mapper

      (3.3.1.GA)

      JBoss Hibernate Common Annotations (3.3.0

      .ga)

      smx@root:osgi>

       

      What am I doing wrong? Also, how can I search for a package on the bus? e.g. match all bundles that have this package in their exported package header?

        • 1. Re: ClassNotFoundException in org.hibernate.annotations.commons.reflection
          stlewis

          You should be able to do "osgi/headers | grep org.hibernate" to double check that the package is exported by some bundle.  Also looking at your pom do you need the second maven-bundle-plugin instance for process-classes?  Have you tried commenting out that second plugin element and rebuilding your bundle to see if that helps?  Also any particular reason you're using version 1.4 of the maven bundle plugin?  You might consider commenting out the "version" element so you'll be using the latest version of the plugin.

          • 2. Re: ClassNotFoundException in org.hibernate.annotations.commons.reflection
            ccustine

            Hibernate is a bit tricky right now due to the way it is commonly packaged, with annotations being a bundle fragment.  Our underlying osgi container (Felix) has only recently supported these fragments, so if you let me know where you acquired the hibernate bundles, I will try to check these versions against the latest Felix and see if it works better than it did a few months ago.

             

            What some users have done is create their own bundle with the fragments included directly in the main hibernate bundle.  You would have to get the exports for the fragments in the manifest of the main hibernate jar, but other than that it shouldn't be too difficult.

             

            Thanks,

            Chris

            • 3. Re: ClassNotFoundException in org.hibernate.annotations.commons.reflection
              oren

              Hi Chris,

              I am working on this exact problem right now. Your reply is very timely. I wish I just had one uber-bundle-jar for all hibernate dependencies, but there doesn't seem to be one (the one at com.google says it's one, but I can't get it to work).

               

              I got all of my bundles and fragments at springsource.com:

               

              1. Main Hibernate bundles

              osgi/install -s mvn:javax.persistence/com.springsource.javax.persistence/1.99.0

              osgi/install -s mvn:javax.transaction/com.springsource.javax.transaction/1.1.0

              osgi/install -s mvn:com.mchange.c3p0/com.springsource.com.mchange.v2.c3p0/0.9.1.2

              osgi/install -s mvn:org.hibernate/com.springsource.org.hibernate/3.3.1.GA

              osgi/install -s mvn:org.hibernate/com.springsource.org.hibernate.annotations.common/3.3.0.ga

               

              1. Fragments

              osgi/install    wrap:mvn:org.springframework.ws/spring-oxm-tiger/1.5.7

              osgi/install    mvn:org.hibernate/com.springsource.org.hibernate.annotations/3.4.0.GA

              osgi/install    mvn:org.hibernate/com.springsource.org.hibernate.ejb/3.4.0.GA

               

              1. May need to restart the bus here so that fragments attach!

               

              (these are installed first to my local repo because they are dependencies of my project poms, but I am sure they can be retrieved directly from the ivy external bundles site).

               

              What happens is that I need to restart the bus (fuse sm 4.1.0.2) after installing the above so that fragments are attached to their hosts (is that normal?), but my core-data project fails to start due to a package duplication:

               

              further-core-query (1.0.0.SNAPSHOT)

              further-core-data (1.0.0.SNAPSHOT)

              Java Transaction API (1.1.0)

              JBoss Hibernate Object-Relational Mapper

              (3.3.1.GA)

                                                     Fragments: 219,220

              JBoss Hibernate Common Annotations (3.3.0

              .ga)

              JBoss Hibernate Annotations (3.4.0.GA)

                                                     Hosts: 217,198,211

              JBoss Hibernate Entity Manager (3.4.0.GA)

               

                                                     Hosts: 217

              smx@root:osgi> start 212

              ERROR CommandLineExecutionFailed: org.apache.geronimo.gshell.command.CommandExce

              ption: org.osgi.framework.BundleException: Unable to resolve due to constraint v

              iolation.

               

              (There's a different thread that explains why the offending package does not appear in the error message above: http://fusesource.com/forums/thread.jspa?messageID=4191)

               

              So I don't know what to do... all I want is to get my project to work with a standard spring-tx + hibernate session factory. No fancy osgi-hibernate stuff like dynamic package loading into hibernate.

               

              Attached please find core-data's and core-query's poms. I think the reason that query works and data doesn't is that data uses more hibernate packages and runs into a duplication problem.

               

              Any help would be greatly appreciated. Thank you so much in advance.

              • 4. Re: ClassNotFoundException in org.hibernate.annotations.commons.reflection
                ccustine

                Hi Oren,

                The problem where you have to restart the ESB is actually a bug in our deployer that hasn't come up yet.  For fragments, if the host bundle was started before the fragment was resolved (which appears to be the case here), the host must be refreshed in order to see the fragment (or as you discovered, you can restart the container to get the same effect).

                 

                Not that this will solve your full problem, but you can try osgi/refresh 217 (whatever the main hibernate ORM bundle ID is).  This should get things resolved without restarting.

                 

                What I will do is create a bug at Apache for the fragment start order issue and see if I can figure out a way to detect if there are any matching fragment/host combinations in a single deploy polling cycle and delay starting the host.

                 

                As far as the hibernate bundle and fragments, if you want it to work with 4.1.0.2 your only real option is to create this uber jar.  I would recommend looking at some of our code that wraps third party jars by using the felix maven plugin and the shade plugin.  The shade plugin will pull in dependencies and flatten them out, and the felix plugin will create the manifest for you.  If I can't get these bundles to work as-is with the latest Felix version slated for ESB 4.2, I think I will commit to doing this uber bundle and making it available via the Fuse maven repo.

                 

                I hope this helps you out, and sorry that there is no easy fix at the moment.  Is it too late to use OpenJPA? 

                 

                Chris

                • 5. Re: ClassNotFoundException in org.hibernate.annotations.commons.reflection
                  oren

                  Hi Chris,

                   

                  This is extremely helpful. Thank you so much.

                   

                  - Refreshing the hosts instead of restarting worked!

                   

                  - Too late to use OpenJPA... we already invested in Hibernate and we have no knowledge of OpenJPA in our development team.

                   

                  - Yes, please commit the uber-jar generated from Felix + shade into the FUSE repo when you can and kindly point me out to it in this thread. That would be wonderful. In the meantime, I will try to do it myself for fun. I am not sure if I need shade - couldn't I just create a maven project with hibernate-core, hibernate-annotations, hibernate-annotations-common as dependencies, and use Felix + Embed-Dependency to achieve the same effect?

                   

                  Thanks!

                  Oren

                  • 6. Re: ClassNotFoundException in org.hibernate.annotations.commons.reflection
                    ccustine

                    I haven't tried Embed-Dependency, but it just might work.  Let us know how it goes.

                     

                    Chris

                    • 7. Re: ClassNotFoundException in org.hibernate.annotations.commons.reflection
                      oren

                      Yes, it works. If I get the fuse hibernate uber-jar to work, I will post the zipped maven project to generate it here. You'll probably beat me to it though. I am slow.

                      • 8. Re: ClassNotFoundException in org.hibernate.annotations.commons.reflection
                        oren

                        Hi, have you made any progress on the Hibernate bundle? This is a nightmare for me right now, it is not working.

                         

                        I did solve some problems using Felix bnd's Embed-Dependency instruction, but that forced me to include hibernate classes in every bundle that uses it (say as a Private-Package). Then there are conflicts between the "org.hibernate.SessionFactory" interfaces that such modules embed separately, and I can't Spring-inject it into my DAOs.

                         

                        Thanks so much in advance.

                        • 9. Re: ClassNotFoundException in org.hibernate.annotations.commons.reflection
                          oren

                          Solved the Hibernate issues using a truly complicated pattern that includes embedding the Hibernate classes into the bundle using the Felix BND maven plugin.

                           

                          There must be a better way to achieve that. Please kindly post a more reusable solution if you develop it. Thank you so much.