title: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource
body: <p>I am getting the exception:</p>
  
  <p><code>Error creating bean with name 'sessionFactory' defined in ServletContext resource</code>  </p>
  
  <p>Is it happening because of jar mismatch? If you need any further details please let me know. </p>
  
  <p><strong>log</strong></p>
  
  <pre><code>ERROR: org.springframework.web.servlet.DispatcherServlet - Context initialization failed
  </code></pre>
  
  <p><strong>Stack-Trace :</strong></p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Unable to load class declared as &lt;mapping class="com.myapp.contact.form.Contact"/&gt; in the configuration:
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:563)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
      at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442)
      at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)
      at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339)
      at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:306)
      at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
      at javax.servlet.GenericServlet.init(GenericServlet.java:160)
      at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1266)
      at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1185)
      at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1080)
      at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5027)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
      at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
      at java.util.concurrent.FutureTask.run(FutureTask.java:138)
      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
      at java.lang.Thread.run(Thread.java:662)
  Caused by: org.hibernate.MappingException: Unable to load class declared as &lt;mapping class="com.myapp.contact.form.Contact"/&gt; in the configuration:
      at org.hibernate.cfg.AnnotationConfiguration.parseMappingElement(AnnotationConfiguration.java:638)
      at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1561)
      at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1540)
      at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:1002)
      at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:69)
      at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1514)
      at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:990)
      at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:69)
      at org.hibernate.cfg.Configuration.configure(Configuration.java:1449)
      at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:978)
      at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:69)
      at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:642)
      at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
      ... 28 more
  </code></pre>
  
  <p><strong>spring-servlet.xml :</strong></p>
  
  <p>
      </p>
  
  <pre><code>&lt;bean id="jspViewResolver"  class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
      &lt;property name="viewClass"
          value="org.springframework.web.servlet.view.JstlView" /&gt;
      &lt;property name="prefix" value="/WEB-INF/jsps/" /&gt;
      &lt;property name="suffix" value=".jsp" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="messageSource"    class="org.springframework.context.support.ReloadableResourceBundleMessageSource"&gt;
      &lt;property name="basename" value="classpath:messages" /&gt;
      &lt;property name="defaultEncoding" value="UTF-8" /&gt;
  &lt;/bean&gt;
  &lt;bean id="propertyConfigurer"   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
      p:location="classpath:/jdbc.properties" /&gt;
  
  &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
      destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
      p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" /&gt;
  
  
  &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="configLocation"&gt;
          &lt;value&gt;classpath:/hibernate.cfg.xml&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="configurationClass"&gt;
          &lt;value&gt;org.hibernate.cfg.AnnotationConfiguration&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;${jdbc.dialect}&lt;/prop&gt;
              &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
              &lt;!-- &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt; --&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;tx:annotation-driven /&gt;
  &lt;bean id="transactionManager"   class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p><strong>pom.xml:</strong></p>
  
  <pre><code>    &lt;properties&gt;
      &lt;org.springframework.version&gt;3.0.5.RELEASE&lt;/org.springframework.version&gt;
  &lt;/properties&gt;
  
  &lt;dependencies&gt;
      &lt;!-- Core utilities used by other modules. Define this if you use Spring 
          Utility APIs (org.springframework.core.*/org.springframework.util.*) --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Expression Language (depends on spring-core) Define this if you use 
          Spring Expression APIs (org.springframework.expression.*) --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-expression&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Bean Factory and JavaBeans utilities (depends on spring-core) Define 
          this if you use Spring Bean APIs (org.springframework.beans.*) --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-beans&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Aspect Oriented Programming (AOP) Framework (depends on spring-core, 
          spring-beans) Define this if you use Spring AOP APIs (org.springframework.aop.*) --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-aop&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Application Context (depends on spring-core, spring-expression, spring-aop, 
          spring-beans) This is the central artifact for Spring's Dependency Injection 
          Container and is generally always defined --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Various Application Context utilities, including EhCache, JavaMail, 
          Quartz, and Freemarker integration Define this if you need any of these integrations --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-context-support&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Transaction Management Abstraction (depends on spring-core, spring-beans, 
          spring-aop, spring-context) Define this if you use Spring Transactions or 
          DAO Exception Hierarchy (org.springframework.transaction.*/org.springframework.dao.*) --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-tx&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- JDBC Data Access Library (depends on spring-core, spring-beans, spring-context, 
          spring-tx) Define this if you use Spring's JdbcTemplate API (org.springframework.jdbc.*) --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-jdbc&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Object-to-Relation-Mapping (ORM) integration with Hibernate, JPA, 
          and iBatis. (depends on spring-core, spring-beans, spring-context, spring-tx) 
          Define this if you need ORM (org.springframework.orm.*) --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Object-to-XML Mapping (OXM) abstraction and integration with JAXB, 
          JiBX, Castor, XStream, and XML Beans. (depends on spring-core, spring-beans, 
          spring-context) Define this if you need OXM (org.springframework.oxm.*) --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-oxm&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Web application development utilities applicable to both Servlet and 
          Portlet Environments (depends on spring-core, spring-beans, spring-context) 
          Define this if you use Spring MVC, or wish to use Struts, JSF, or another 
          web framework with Spring (org.springframework.web.*) --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Spring MVC for Servlet Environments (depends on spring-core, spring-beans, 
          spring-context, spring-web) Define this if you use Spring MVC with a Servlet 
          Container such as Apache Tomcat (org.springframework.web.servlet.*) --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Spring MVC for Portlet Environments (depends on spring-core, spring-beans, 
          spring-context, spring-web) Define this if you use Spring MVC with a Portlet 
          Container (org.springframework.web.portlet.*) --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-webmvc-portlet&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Support for testing Spring applications with tools such as JUnit and 
          TestNG This artifact is generally always defined with a 'test' scope for 
          the integration testing framework and unit testing stubs --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-test&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;!-- Spring security --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
          &lt;artifactId&gt;spring-security-core&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
          &lt;artifactId&gt;spring-security-web&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
          &lt;artifactId&gt;spring-security-taglibs&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
          &lt;artifactId&gt;spring-security-config&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
  
      &lt;!-- Hibernate resources --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
          &lt;version&gt;3.6.7.Final&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
          &lt;version&gt;3.1.0.GA&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-annotations&lt;/artifactId&gt;
          &lt;version&gt;3.3.1.GA&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
          &lt;version&gt;3.3.2.GA&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;!-- EhCache --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;net.sf.ehcache&lt;/groupId&gt;
          &lt;artifactId&gt;ehcache-core&lt;/artifactId&gt;
          &lt;version&gt;2.2.0&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;antlr&lt;/groupId&gt;
          &lt;artifactId&gt;antlr&lt;/artifactId&gt;
          &lt;version&gt;2.7.7&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Taglibs --&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;taglibs&lt;/groupId&gt;
          &lt;artifactId&gt;standard&lt;/artifactId&gt;
          &lt;version&gt;1.1.2&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
          &lt;artifactId&gt;jstl&lt;/artifactId&gt;
          &lt;version&gt;1.1.2&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;commons-dbcp&lt;/groupId&gt;
          &lt;artifactId&gt;commons-dbcp&lt;/artifactId&gt;
          &lt;version&gt;20030825.184428&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;commons-pool&lt;/groupId&gt;
          &lt;artifactId&gt;commons-pool&lt;/artifactId&gt;
          &lt;version&gt;20030825.183949&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Log4j --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;log4j&lt;/groupId&gt;
          &lt;artifactId&gt;log4j&lt;/artifactId&gt;
          &lt;version&gt;1.2.14&lt;/version&gt;
          &lt;type&gt;jar&lt;/type&gt;
          &lt;exclusions&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;javax.mail&lt;/groupId&gt;
                  &lt;artifactId&gt;mail&lt;/artifactId&gt;
              &lt;/exclusion&gt;
          &lt;/exclusions&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
          &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;
          &lt;version&gt;1.6.0&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;com.oracle&lt;/groupId&gt;
          &lt;artifactId&gt;ojdbc14&lt;/artifactId&gt;
          &lt;version&gt;10.2.0.1.0&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
          &lt;artifactId&gt;servlet-api&lt;/artifactId&gt;
          &lt;version&gt;2.4&lt;/version&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;javax.servlet.jsp&lt;/groupId&gt;
          &lt;artifactId&gt;jsp-api&lt;/artifactId&gt;
          &lt;version&gt;2.1&lt;/version&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;javax.validation&lt;/groupId&gt;
          &lt;artifactId&gt;validation-api&lt;/artifactId&gt;
          &lt;version&gt;1.0.0.GA&lt;/version&gt;
      &lt;/dependency&gt;
  
  
  &lt;/dependencies&gt;
  </code></pre>
  
  <p>After digging a lot I found that it is happening because of ClassNotFound exception for AnnotationSessionFactoryBean but I have added the dependencies and it is in my class path so why I am getting this kind of exception. screen shots attached 
  <img src="https://i.stack.imgur.com/tu3i2.png" alt="enter image description here">
  <img src="https://i.stack.imgur.com/bU9Eo.png" alt="enter image description here">
  Thanks.....</p>