title: NoSuchBeanDefinitionException when using Spring and Hibernate
body: <p>I get a NoSuchBeanDefinitionException when trying to run my program (: No bean named 'org.springframework.orm.hibernate4.LocalSessionFactoryBean' is defined)  Yet the bean has been defined in the Beans2.xml file. The classes are listed below. Thanks.</p>
  
  <p>PieceHibernateDAO.java</p>
  
  <pre><code>package sample.spring.chapter01;
  
  import java.io.BufferedReader;
  import java.io.FileNotFoundException;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.util.ArrayList;
  import java.util.List;
  
  import org.apache.commons.csv.CSVFormat;
  import org.apache.commons.csv.CSVParser;
  import org.apache.commons.csv.CSVRecord;
  import org.hibernate.SessionFactory;
  import org.springframework.core.io.Resource;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Repository;
  
  import org.springframework.transaction.TransactionStatus;
  //import org.springframework.transaction.TransactionCallback;
  //import org.springframework.transaction.TransactionTemplate;
  
  
  
  @Repository(value="PieceHibernateDAO")
  public class PieceHibernateDAO {
  
      @Autowired
      private SessionFactory sessionFactory;
  
  
      public int createPieceHibernate(final Piece piece) {
          sessionFactory.getCurrentSession().save(piece);
          return piece.getpieceid();
      }
  
      //public Piece getPiece
  
  
      public void fill(Resource resource) throws FileNotFoundException, IOException{
          InputStream is=resource.getInputStream();
          BufferedReader br = new BufferedReader(new InputStreamReader(is));              
  
  
          CSVParser parser = CSVFormat.DEFAULT.withHeader().parse(br);
  
          for (CSVRecord record:parser) {
              String ref=record.get("référence ascensoriste").trim();
              String asc=record.get("ascensoriste").trim();
              //String desc=record.get("Description article").trim();                
              String prix=record.get("Pv");
              String category=record.get("Familie").trim();
              //System.out.println(category);
              Piece lift_comp=new Piece();
  
  
  
  
              lift_comp.setasc(asc);
             //lift_comp.setdesc(desc);
              lift_comp.setref(ref);
              lift_comp.setprice(prix);
              lift_comp.settype(category);
              lift_comp.setinfo();
              //listp.add(lift_comp);
              int cpid=lift_comp.getcompanyid(asc);
              lift_comp.setcompanyid(cpid);
  
              //sessionFactory.getCurrentSession().save(lift_comp);
              createPieceHibernate(lift_comp);
          }
  
  
      }
  
  
  
  } 
  </code></pre>
  
  <p>Beans2.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;beans xmlns = "http://www.springframework.org/schema/beans"
     xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation = "http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&gt;
  
      &lt;bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="packagesToScan" value="sample.spring" /&gt;
      &lt;/bean&gt;
  
      &lt;bean class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close" id="dataSource"&gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
          &lt;property name="url" value="jdbc:mysql://localhost:3306/liftcomp_db" /&gt;
          &lt;property name="username" value="root" /&gt;
          &lt;property name="password" value="8K887h))00" /&gt;
      &lt;/bean&gt;
  
  
   &lt;bean id="PieceHibernateDAO" 
   class="sample.spring.chapter01.PieceHibernateDAO"&gt;
          &lt;property name="sessionFactory" ref="org.springframework.orm.hibernate4.LocalSessionFactoryBean"/&gt;
      &lt;/bean&gt;
  
  
  
    &lt;bean id="PieceDAO" class="sample.spring.chapter01.PieceDAO"&gt;
          &lt;property name="listp" ref="Piece"&gt;&lt;/property&gt;              
     &lt;/bean&gt;
     &lt;bean id = "Company" class = "sample.spring.chapter01.Company"/&gt;
     &lt;bean id = "Family" class = "sample.spring.chapter01.Family"/&gt;
     &lt;bean id = "Piece" class = "sample.spring.chapter01.Piece"/&gt;   
  &lt;/beans&gt;
  </code></pre>