title: Spring OpenSessionInViewFilter: can't save object
body: <p>I encountered an common error with spring + hibernate, but can't fix it. I have 2 tables with one-to-many relation: each <strong>Bsc</strong> have many <strong>Cell</strong></p>
  
  <p>My hibernate config:</p>
  
  <pre><code>&lt;class name="Bsc" table="bsc"&gt;
      &lt;id name="id" column="id"&gt;
          &lt;generator class="native" /&gt;
      &lt;/id&gt;
      &lt;property name="name" /&gt;
      &lt;set name="cells"&gt;
          &lt;key column="bsc_id" /&gt;
          &lt;one-to-many class="Cell" /&gt;
      &lt;/set&gt;
  &lt;/class&gt;
  
      &lt;class name="Cell" table="cell"&gt;
      &lt;id name="id" column="id"&gt;
          &lt;generator class="native" /&gt;
      &lt;/id&gt;
      &lt;property name="name" column="name" /&gt;
      &lt;many-to-one name="bsc" column="bsc_id" not-null="true" /&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p>My dao:</p>
  
  <pre><code>public class BscDaoImpl extends HibernateDaoSupport implements BscDao {
  
  @Override
  public Bsc get(int id) {
      return (Bsc) getHibernateTemplate().get(Bsc.class, id);
  }
  
  @Override
  public void save(Bsc bsc) {
      getHibernateTemplate().saveOrUpdate(bsc);
  }
  }
  </code></pre>
  
  <p>My controller:</p>
  
  <pre><code>public class BscFormController extends SimpleFormController {
  
  private BscDao bscDao;
  
  public void setBscDao(BscDao bscDao) {
      this.bscDao = bscDao;
  }
  
  protected Object formBackingObject(HttpServletRequest request)
          throws Exception {
      String id = request.getParameter("id");
  
      if (!StringUtils.isBlank(id)) {
          return bscDao.get(new Integer(id));
      }
  
      return new Bsc();
  }
  
  public ModelAndView onSubmit(HttpServletRequest request,
          HttpServletResponse response, Object command, BindException errors)
          throws Exception {
  
      Bsc bsc = (Bsc) command;
      String success = getSuccessView();
  
      bscDao.save(bsc);
  
      return new ModelAndView(success);
  }
  }
  </code></pre>
  
  <p>In my view, I have a form to create/edit bsc info. In addition, I want list all cells of this bsc, so I config lazy load in web.xml</p>
  
  <pre><code>&lt;filter&gt;
      &lt;filter-name&gt;openSessionInViewFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.orm.hibernate3.support.OpenSessionInViewFilter&lt;/filter-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;singleSession&lt;/param-name&gt;
          &lt;param-value&gt;true&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;flushMode&lt;/param-name&gt;
          &lt;param-value&gt;AUTO&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;sessionFactoryBeanName&lt;/param-name&gt;
          &lt;param-value&gt;mySessionFactory&lt;/param-value&gt;
      &lt;/init-param&gt;
  &lt;/filter&gt;
  </code></pre>
  
  <p>My problem: I can't save object when press Save button (it's saved ok if I remove OpenSessionInViewFilter config).</p>
  
  <hr>
  
  <p>I have added TransactionManager config:</p>
  
  <pre><code>&lt;bean id="transactionManager"     class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="mySessionFactory"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>But it still doesn't work. Did I miss something ?</p>