title: hibernate 4 sessionfactory packagestoscan property usage
body: <p>We have a web application based on Spring 3.2.4 and Hibernate 4.2.3. It was generated using the appfuse 2.2.1 archetype. As per the documentation available, we can use the packagesToScan property of the SessionFactory to dynamically pick up entity classes and set up the sessionFactory. However, we face an issue that we have to explicitly mention the entity class name in hibernate.cfg.xml for it to be recognized by hibernate. 
  I could not find any appropriate answers on forums, so posting here. It may be that my understanding of how it works may be incorrect, so any pointers are welcome. </p>
  
  <p>Thanks</p>
  
  <p><em><strong>applicationContext.xml</em></strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans -- namespace declarations here
         default-lazy-init="true"&gt;
       &lt;!-- Activates scanning of @Autowired --&gt;
       &lt;context:annotation-config/&gt;
       &lt;!-- Activates scanning of @Repository and @Service --&gt;
       &lt;context:component-scan base-package="com.xyz"/&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p><em><strong>hibernate.cfg.xml</em></strong></p>
  
  <pre><code>&lt;!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
   &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
         &lt;mapping class="com.xyz.model.Address" /&gt;
         &lt;mapping class="com.xyz.model.Company" /&gt;
         &lt;mapping class="com.xyz.model.CompanyBilling" /&gt;
         &lt;!-- BIG LIST OF MY ENTITY CLASSES --&gt;
      &lt;/session-factory&gt;
   &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p><em><strong>applicationContext-dao.xml</em></strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans -- namespace declarations here
        default-lazy-init="true"&gt;
      &lt;bean class="org.springframework.orm.hibernate4.HibernateExceptionTranslator"/&gt;
      &lt;bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/&gt;
      &lt;!-- Hibernate SessionFactory --&gt;
      &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" destroy-method="destroy"&gt;
          &lt;property name="dataSource" ref="dataSource"/&gt;
          &lt;property name="configLocation" value="classpath:hibernate.cfg.xml"/&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                   &lt;prop key="hibernate.dialect"&gt;${hibernate.dialect}&lt;/prop&gt;
                   &lt;prop key="hibernate.query.substitutions"&gt;true 'Y', false 'N'&lt;/prop&gt;
                   &lt;prop key="hibernate.cache.use_second_level_cache"&gt;true&lt;/prop&gt;
                   &lt;prop key="hibernate.cache.provider_class"&gt;org.hibernate.cache.EhCacheProvider&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
          &lt;property name="packagesToScan"&gt;&lt;list&gt;&lt;value&gt;com.xyz.model&lt;/value&gt;&lt;/list&gt;&lt;/property&gt;
      &lt;/bean&gt;
  &lt;!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) --&gt;
      &lt;bean id="transactionManager"  class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory"/&gt;
      &lt;/bean&gt;
      &lt;!-- Activates scanning of @Autowired --&gt;
      &lt;context:annotation-config/&gt;
      &lt;!-- Activates scanning of @Repository --&gt;
      &lt;context:component-scan base-package="com.xyz.dao"/&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p><em><strong>Company.java</em></strong></p>
  
  <pre><code> package com.xyz.model;
   // imports
   @Entity
   @Table(name = "company")
   @SQLDelete(sql = "UPDATE company SET activeFlag = 0, lastupdated=now() WHERE id = ?")
   @Where(clause = "1=activeflag")
   @Component
   public class Company extends BaseEntity implements java.io.Serializable {
      // properties and getter setters
   }
  </code></pre>