title: How to resolve Error creating bean with name 'sessionFactory' defined in ServletContext resource in Hibernate4
body: <p>I am using Hibernate4 in my application for multitenancy implementation while building application war i got following error in my catalina of tomcat.</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: 
  protected org.hibernate.SessionFactory com.boxco.blip.dao.BaseDao.sessionFactory; 
  nested exception is org.springframework.beans.factory.BeanCreationException: 
  Error    creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext-persistence.xml]: 
  Invocation of init method failed; nested exception is java.lang.NullPointerException
  </code></pre>
  
  <p>Here is my application -persistance.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="..."&gt;
  
  &lt;context:annotation-config /&gt;
  &lt;tx:annotation-driven /&gt;
  &lt;context:component-scan base-package="com.boxco.blip"&gt;
  &lt;/context:component-scan&gt;
  
  &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
  &lt;bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"&gt;
      &lt;property name="jndiName"&gt;
          &lt;value&gt;jdbc/blip&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="resourceRef"&gt;
          &lt;value&gt;true&lt;/value&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  
  &lt;!-- Hibernate Session Factory  --&gt;
  &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource"/&gt;
      &lt;property name="annotatedClasses"&gt;
          &lt;list&gt;
      &lt;value&gt;com.boxco.blip.model.Address&lt;/value&gt; 
          &lt;/list&gt;
      &lt;/property&gt;
      &lt;property name="hibernateProperties"&gt;          
           &lt;map&gt;
              &lt;entry key="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" /&gt;
              &lt;entry key="hibernate.hbm2ddl.auto" value="validate" /&gt;
              &lt;entry key="hibernate.show_sql" value="true" /&gt;
              &lt;entry key="hibernate.connection.autocommit" value="false" /&gt;
              &lt;entry key="hibernate.jdbc.batch_size" value="0" /&gt;
              &lt;entry key="hibernate.connection.release_mode" value="after_transaction" /&gt;
              &lt;entry key="hibernate.multiTenancy" value="SCHEMA" /&gt;
              &lt;entry key="hibernate.tenant_identifier_resolver" value-ref="multiTenantIdentifierResolver1" /&gt;
              &lt;entry key="hibernate.multi_tenant_connection_provider" value-ref="multiTenantConnectionProvider1" /&gt;      
        &lt;/map&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
   &lt;bean id="multiTenantConnectionProvider1" class="com.boxco.blip.web.util.SchemaMultiTenantConnectionProviderBlip"&gt;
      &lt;property name="dataSource" ref="dataSource"/&gt;
    &lt;/bean&gt;
   &lt;bean id="multiTenantIdentifierResolver1" class="com.boxco.blip.web.util.SchemaCurrentTenantIdentifierResolverBlip" /&gt;
  
  &lt;bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory"/&gt;
      &lt;property name="dataSource" ref="dataSource"/&gt;
  &lt;/bean&gt;    
  </code></pre>
  
  <p>
  MY AddressDao.java File</p>
  
  <pre><code>package com.boxco.blip.dao;
  
  import com.boxco.blip.model.Address;
  
  public interface AddressDao {
     public Address getAddressByAddressId(int addressId);
   }
  </code></pre>
  
  <p>My AddressDaoImpl.java file</p>
  
  <pre><code>package com.boxco.blip.dao;
  
  import org.apache.log4j.Logger;
  import org.hibernate.Criteria;
  import org.hibernate.criterion.Restrictions;
  import org.springframework.stereotype.Component;
  import org.springframework.stereotype.Repository;
  
  import com.boxco.blip.model.Address;
  @Component
  @Repository(value = "addressDao")
  
  public class AddressDaoImpl extends BaseDao implements AddressDao {
  private static final Logger logger = Logger.getLogger(AddressDaoImpl.class);
  
      @Override
   public Address getAddressByAddressId(int addressId) {
      logger.info("Entering getAddressByAddressId()........");
      Address address = null;
      Criteria criteria = getSession().createCriteria(Address.class);
      criteria.add(Restrictions.eq("addressId", addressId));
      criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
      address = (Address)criteria.uniqueResult();
      logger.info("Exiting getAddressByAddressId()..........");
      return address;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>My Base Dao file where i init sessionFactory object:</p>
  
  <pre><code>package com.boxco.blip.dao;
  
  import java.sql.SQLException;
  import java.util.Iterator;
  import java.util.Map;
  import java.util.Map.Entry;
  import java.util.Set;
  
  import org.hibernate.Criteria;
  import org.hibernate.HibernateException;
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.hibernate.criterion.Restrictions;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.annotation.Qualifier;
  import org.springframework.orm.hibernate3.HibernateTemplate;
  
  import com.mysql.jdbc.Connection;
  
  public abstract class BaseDao 
   {
  
  @Autowired
  @Qualifier("sessionFactory")
  protected SessionFactory sessionFactory;
  
  protected void init(SessionFactory factory) {
      try {
          setSessionFactory(factory);
      } catch (Exception e) {
          e.printStackTrace();
      }
  
  }
  
  public SessionFactory getSessionFactory() {
      return sessionFactory;
  }
  
  public void setSessionFactory(SessionFactory sessionFactory) {
      this.sessionFactory = sessionFactory;
  }
  
  public Session getSession() throws HibernateException {
      // return sessionFactory.getCurrentSession();
      return sessionFactory.withOptions().tenantIdentifier("BLIP1314ERP")
              .openSession();
  }
  
       //some more methods
    }
  </code></pre>
  
  <p>My Dispatcher-servlet.xml file where   is added</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="..."&gt;
  
  &lt;mvc:annotation-driven/&gt;
  
  &lt;context:component-scan base-package="com.boxco"&gt;
      &lt;!-- &lt;context:exclude-filter type="regex" expression="com.boxco.blip.dao.*" /&gt; --&gt;
  &lt;/context:component-scan&gt;
  
  &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver"&gt;
      &lt;property name="basenames" value="views"/&gt;
  
  &lt;bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"&gt;
      &lt;property name="basenames"&gt;
          &lt;value&gt;messages,error_codes&lt;/value&gt;
      &lt;/property&gt;
  &lt;/bean&gt; 
  
  &lt;bean id="validatorFactory"
        class="org.springmodules.validation.commons.DefaultValidatorFactory"&gt;
      &lt;property name="validationConfigLocations"&gt;
          &lt;list&gt;
              &lt;value&gt;/WEB-INF/validator-rules.xml&lt;/value&gt;
              &lt;value&gt;/WEB-INF/validator.xml&lt;/value&gt;
              &lt;value&gt;/WEB-INF/validatorFas.xml&lt;/value&gt;
              &lt;value&gt;/WEB-INF/validatorFas-rules.xml&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="validator" class="org.springmodules.validation.commons.DefaultBeanValidator"&gt;
      &lt;property name="validatorFactory" ref="validatorFactory"/&gt;
  &lt;/bean&gt;     
  </code></pre>
  
  <p>Error is in my <code>SessionFactory</code> initialization but why it is not getting init is my problem..?
  (I am handling this error first time)..</p>
  
  <p>Any suggestion appreciated.</p>