Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.ClassNotFoundException: org.hibernate.ejb.HibernatePersistence

Tags:

I'm trying to deploy an application in the following environment.

  • JPA 2.0
  • Spring 3.2.2
  • MySQL 5.6.11

My configurations until now in the application-context.xml file are as under.

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xmlns:p="http://www.springframework.org/schema/p"        xmlns:aop="http://www.springframework.org/schema/aop"        xmlns:tx="http://www.springframework.org/schema/tx"        xmlns:context="http://www.springframework.org/schema/context"        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">       <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">         <property name="persistenceUnitName" value="WebAppPU"/>         <!--<property name="jpaVendorAdapter">             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">                 <property name="showSql" value="true"/>                 <property name="generateDdl" value="true"/>                 <property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect"/>             </bean>         </property>-->     </bean>      <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>      <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">         <property name="entityManagerFactory" ref="entityManagerFactory"/>     </bean>      <tx:annotation-driven transaction-manager="transactionManager"/>      <bean id="testDAOService" class="admin.dao.TestDAO"/> </beans> 

This works fine until the commented lines in the above xml file are uncommented. When these lines are uncommented, I'm getting the following exception.

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot create inner bean 'org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter#932760' of type [org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter] while setting bean property 'jpaVendorAdapter'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter#932760' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: 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.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:282)     at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:121)     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1393)     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1134)     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:608)     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)     at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)     at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)     at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)     at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4797)     at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5291)     at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)     at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)     at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)     at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)     at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:657)     at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:536)     at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1462)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     at java.lang.reflect.Method.invoke(Method.java:601)     at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)     at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)     at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:791)     at org.apache.catalina.manager.ManagerServlet.check(ManagerServlet.java:1445)     at org.apache.catalina.manager.ManagerServlet.deploy(ManagerServlet.java:860)     at org.apache.catalina.manager.ManagerServlet.doGet(ManagerServlet.java:357)     at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)     at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)     at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)     at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:581)     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)     at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)     at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)     at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)     at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1822)     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)     at java.lang.Thread.run(Thread.java:722) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter#932760' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: 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:1013)     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:959)     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:490)     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)     at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:271)     ... 55 more 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:1006)     ... 59 more 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:525)     at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)     ... 61 more Caused by: java.lang.ClassNotFoundException: org.hibernate.ejb.HibernatePersistence     at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)     at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)     ... 67 more 

I have tried to add hibernate-entity-manger-4.2.0.jar to the classpath but that caused another exception java.lang.ClassNotFoundException: org.hibernate.HibernateException.

So, which jar files are needed exactly here? Is it dependent upon the Hibernate framework which I'm not using in this application?


The persistence.xml file looks like the following.

<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">   <persistence-unit name="WebAppPU" transaction-type="RESOURCE_LOCAL">     <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>     <class>model.Test</class>     <exclude-unlisted-classes>false</exclude-unlisted-classes>     <properties>       <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/social_networking?zeroDateTimeBehavior=convertToNull"/>       <property name="javax.persistence.jdbc.password" value="root"/>       <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>       <property name="javax.persistence.jdbc.user" value="root"/>     </properties>   </persistence-unit> </persistence> 

Update :

Quick reply : (as the answers below do not directly address the cause of the exception)

The cause of the stated exception was (most probably) that I misconfigured databasePlatform as org.hibernate.dialect.HSQLDialect in the given XML file though I was using MySQL 5.x.

It should have actually been org.hibernate.dialect.MySQLDialect instead (or version-restricted/version-specific - org.hibernate.dialect.MySQL5Dialect).

Disclaimer : I do not remember precisely the exact cause of the exception after more than one year but most probably (99.99%) this misconfiguration was the culprit.

like image 532
Tiny Avatar asked May 07 '13 19:05

Tiny


1 Answers

If you look at spring-orm maven dependency you will see that it depends on hibernate-entitymanager which is the jar that contains the class not found.

http://mvnrepository.com/artifact/org.springframework/spring-orm/3.1.1.RELEASE

Add those dependencies to your maven configuration file.

like image 173
fmodos Avatar answered Oct 21 '22 08:10

fmodos