title: How to enable hibernate filter for sessionFactory.getCurrentSession()?
body: <p>Say there is a User table with structure:</p>
  
  <p><strong>User</strong></p>
  
  <ul>
  <li>List item</li>
  <li>userId (PK)</li>
  <li>company (PK)</li>
  <li>userName </li>
  <li>address ...etc</li>
  </ul>
  
  <p>And I want to retrieve users only for the current company (company can be changed by the user through UI, so the company is a runtime parameter)</p>
  
  <p>Similarly there are many other tables that have similar structure with common column (company), and I want to restrict data to only the current company, so I am using hibernate filter to filter out data.</p>
  
  <p>Hibernate annotations:</p>
  
  <pre><code>&lt;bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
  
      &lt;property name="dataSource"&gt;
          &lt;ref bean="dataSource" /&gt;
      &lt;/property&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;Dialect....&lt;/prop&gt;
              &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
              &lt;prop key="hibernate.generate_statistics"&gt;true&lt;/prop&gt;
              &lt;prop key="hibernate.connection.release_mode"&gt;after_transaction&lt;/prop&gt;
              &lt;prop key="hibernate.cache.use_second_level_cache"&gt;false&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
      &lt;property name="annotatedClasses"&gt;
          &lt;list&gt;
              &lt;value&gt;User&lt;/value&gt;
          .....
          &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Filter definitions:</p>
  
  <pre>
  @org.hibernate.annotations.FilterDef(name="restrictToCurrentCompany",
      parameters = {@org.hibernate.annotations.ParamDef(
              name = "currentCompanyNumber", type = "int"
          )
      }
  )
  @Entity
  @Table(name = "USER")
  @org.hibernate.annotations.Filter(
          name = "restrictToCurrentCompany",
          condition="company = :currentCompanyNumber"
  )
  public class User implements Serializable {
      private int company;
      private String userName;
      ...etc..
  }
  </pre>
  
  <p>Dao's:</p>
  
  <pre>
  
  @Repository
  @Transactional(readOnly = true)
  public class UserDAOImpl implements UserDAO {
  
      @Autowired(required = true)
      private SessionFactory sessionFactory;
  
      public Set getUsers(){
          .....Criteria queries to retrieve users for the current company     
      }
  
      private Session getSession(){
          return sessionFactory.getCurrentSession();
      }
  
  }
  </pre>
  
  <p>If I change the getSession like so;</p>
  
  <pre><code>private Session getSession(){
      Session session = sessionFactory.getCurrentSession();
      Filter filter = session.enableFilter("restrictToCurrentCompany");
      filter.setParameter("currentCompanyNumber", UserUtils.getCurrentCompany());
      return sessionFactory.getCurrentSession();
  }
  </code></pre>
  
  <p>then I can enable the filter and everything looks good, but instead of enabling the filter during getting session is there a simpler alternative to apply and enable filter for the whole session factory/application level? If so, how could I do so using spring configuration?</p>
  
  <p>I tried hooking into hibernate interceptors (pre-load event listerns) but I am bit unsure if this is a correct approach or should I rather use the getSession method listed above to enable filters?</p>