HibernatePersistence class not found exception with JPA + Hibernate + OSGI

user1926519 picture user1926519 · Dec 21, 2014 · Viewed 21.4k times · Source

I am facing problem with jpa + hibernate in equinox environment. I am getting exception

    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter#53308cd' defined in OSGi resource[classpath:/spring/gdl-commons-db-jpa-context.xml|bnd.id=177|bnd.sym=gdl-open-domain]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/hibernate/ejb/HibernatePersistence
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1037)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:983)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:271)
    ... 34 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/hibernate/ejb/HibernatePersistence
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1030)
    ... 38 common frames omitted
Caused by: java.lang.NoClassDefFoundError: org/hibernate/ejb/HibernatePersistence
    at org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter.<init>(HibernateJpaVendorAdapter.java:57)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)
    ... 40 common frames omitted

Please find below about my jars details

    com.springsource.javassist-3.15.0.GA.jar                           
hibernate-core-3.3.2.GA.jar              
com.springsource.javax.transaction-1.1.0.jar                       
hibernate-core-4.1.9.Final.jar           
com.springsource.org.hibernate-4.1.0.Final.jar                     
com.springsource.org.hibernate.annotations.common-3.3.0.ga.jar     
hibernate-entitymanager-3.4.0.GA.jar     
com.springsource.org.hibernate.annotations.common-4.0.1.Final.jar  
hibernate-entitymanager-4.1.9.Final.jar                                
hibernate-annotations-3.4.0.GA.jar                                 
hibernate-jpa-2.0-api-1.0.1.Final.jar    
spring-data-commons-1.6.0.RELEASE.jar
hibernate-validator-4.0.2.GA.jar

My manifest.mf is

    Import-Package: org.springframework.data.jpa.domain.support;version="[
 1.3,2)",org.springframework.data.jpa.repository;version="[1.3,2)",org
 .springframework.data.repository;version="[1.5,2)",javax.sql,org.apac
 he.commons.pool;version="[1.5,2)",org.apache.commons.pool.impl;versio
 n="[1.5,2)",org.springframework.orm.jpa.persistenceunit,org.springfra
 mework.core.io,org.springframework.core.type.classreading,org.springf
 ramework.core.io.support,org.springframework.core.type.filter,org.spr
 ingframework.util,org.springframework.core.type,javax.persistence;version="1.0.1";jpa
 ="2.0",javassist.util.proxy;resolution:=optional,org.
 hibernate,org.springframework.orm.jpa,org.hibernate.ejb,org.springfra
 mework.orm.jpa.vendor,org.springframework.dao.annotation,org.springfr
 amework.jmx.export.assembler,org.springframework.jmx.support,org.spri
 ngframework.beans.factory.config,org.springframework.beans.factory.as
 pectj,org.hibernate.proxy,org.springframework.aop.aspectj.autoproxy,o
 rg.springframework.aop.interceptor,org.hibernate.ejb.connection,javax.persistence.criteria

Meta-Persistence: META-INF/persistence.xml
Spring-Context: spring/*.xml

Please help me to solve this class not found exception.

Thank you,

gopy

Edited:

Still i am facing problem now i am using hibernate-release-4.3.7.Final,

Okay let me explain my project structure and give me clue where i am missing,

Currently our system having one war file inside so many jar files. So i need to migrate to osgi and take out all business related jar to setup module concept.

So initially i take out some important library and converted to as bundle.

Lets say,

Bundle_A is a main bundle and inside contain lots of third party dependency including our application related library library_b, library_c etc.

All library imported as Bundle-ClassPath from Bundle_A,

library_b, library_c having own spring.xml with some configuration which are loading from Bundle_A

library_b, spring.xml having JPARepositoryImpl and configuration, We have some repositories in Bundle_A also referencing library_a implementation. Seems i am getting exception like JPARepositoryImpl class is not loading properly. Class not found exception.

Find some exception below please,

nested exception is java.lang.NoClassDefFoundError: com.domain.jpa.impl.MyRepositoryFactoryBean not found from bundle [My  Platform Domain (My--domain)]
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [org.springframework.data.jpa.domain.support.AuditingEntityListener] for bean with name 'org.springframework.data.jpa.domain.support.AuditingEntityListener' defined in null: problem with class file or dependent class; nested exception is java.lang.NoClassDefFoundError: org.springframework.data.jpa.domain.support.AuditingEntityListener not found from bundle [My  Platform Domain (My--domain)]
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [com.domain.jpa.impl.MyRepositoryFactoryBean] for bean with name 'communicationSettingRepository' defined in null: problem with class file or dependent class; nested exception is java.lang.NoClassDefFoundError: com.domain.jpa.impl.MyRepositoryFactoryBean not found from bundle [My  Platform Domain (My--domain)]
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [com.domain.jpa.impl.MyRepositoryFactoryBean]

.. .. .. etc

This is huge exception because i have lots of repository such as communicationSettingRepository, so its printing all repository name such as

Error loading class [com.domain.jpa.impl.MyRepositoryFactoryBean] for bean with name 'repository' defined in null

end of exception is

Caused by: java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy
    at sun.reflect.annotation.AnnotationParser.parseClassArray(AnnotationParser.java:715)
    at sun.reflect.annotation.AnnotationParser.parseArray(AnnotationParser.java:522)
    at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:348)
    at sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:283)
    at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:117)
    at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:70)
    at java.lang.Class.initAnnotationsIfNecessary(Class.java:3271)
    at java.lang.Class.initAnnotationsIfNecessary(Class.java:3278)
    at java.lang.Class.getAnnotation(Class.java:3219)
    at java.lang.Class.isAnnotationPresent(Class.java:3232)
    at org.hibernate.annotations.common.reflection.java.JavaAnnotationReader.isAnnotationPresent(JavaAnnotationReader.java:50)
    at org.hibernate.annotations.common.reflection.java.JavaXAnnotatedElement.isAnnotationPresent(JavaXAnnotatedElement.java:60)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.syncAnnotatedClasses(Configuration.java:3780)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1411)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:852)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:845)
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:844)
    at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:152)
    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:67)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:288)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1571)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1509)
    ... 31 common frames omitted

Hope you guys can guess actuall issue? Please help me to solve this.

Answer

Andy Dufresne picture Andy Dufresne · Dec 22, 2014

hibernate-entitymanager-4.1.9.Final.jar should the missing class. You have multiple versions of hibernate libraries in your classpath. 3.x and 4.x. I suggest you to download the latest hibernate version and replace them with the current classpath jars.

Also as a side note org/hibernate/ejb/HibernatePersistence is deprecated. org.hibernate.jpa.HibernatePersistenceProvider is the right persistence provider to be used if you are using JPA standard.