title: Spring MVC, generating a form backing object from a request?
body: <p>I am using Spring MVC 2.5, and I am trying to get a JSTL form object to load from a GET request. I have Hibernate POJOs as my backing objects.</p>
  
  <p>There is one page directing to another page with a class id (row primary key) in the request. The request looks like "newpage.htm?name=RowId". This is going into a page with a form backing object,</p>
  
  <p>The newpage above, loads the fields of the object into editable fields, populated with the existing values of the row. The idea is, that you should be able to edit these fields and then persist them back into the database.</p>
  
  <p>The view of this page looks something like this </p>
  
  <pre><code>&lt;form:form commandName="thingie"&gt;
      &lt;span&gt;Name:&lt;/span&gt;
      &lt;span&gt;&lt;form:input path="name" /&gt;&lt;/span&gt;
      &lt;br/&gt;
      &lt;span&gt;Scheme:&lt;/span&gt;
      &lt;span&gt;&lt;form:input path="scheme" /&gt;&lt;/span&gt;
      &lt;br/&gt;
      &lt;span&gt;Url:&lt;/span&gt;
      &lt;span&gt;&lt;form:input path="url" /&gt;&lt;/span&gt;
      &lt;br/&gt;
      &lt;span&gt;Enabled:&lt;/span&gt;
      &lt;span&gt;&lt;form:checkbox path="enabled"/&gt;&lt;/span&gt;
      &lt;br/&gt;
  
      &lt;input type="submit" value="Save Changes" /&gt;
  &lt;/form:form&gt;
  </code></pre>
  
  <p>The controller has this in it,</p>
  
  <pre><code>public class thingieDetailController extends SimpleFormController {
  
      public thingieDetailController() {    
          setCommandClass(Thingie.class);
          setCommandName("thingie");
      }
  
      @Override
      protected Object formBackingObject(HttpServletRequest request) throws Exception {
          Thingie thingieForm = (Thingie) super.formBackingObject(request);
  
          //This output is always null, as the ID is not being set properly
          logger.debug("thingieForm.getName(): [" + thingieForm.getName() + "]");
          //thingieForm.setName(request.getParameter("name"));
          SimpleDAO.loadThingie(thingieForm);
  
          return thingieForm;
      }
  
      @Override
      protected void doSubmitAction(Object command) throws Exception {            
          Thingie thingie = (Thingie) command;
          SimpleDAO.saveThingie(thingie);
      }
  }
  </code></pre>
  
  <p>As you can see from the commented code, I've tried manually setting the object id (name is this case) from the request. However Hibernate complains about the object being desynched when I try and persist the data in the form.</p>
  
  <pre><code>org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)
  </code></pre>
  
  <p>This error seems to do something to the entire session, which stops working for my entire web application, continually throwing the Stale Object State Exception seen above.</p>
  
  <p>If anyone familiar with Spring MVC can help me with this or suggest a workaround, I would really appreciate it.</p>
  
  <p><strong>EDIT:</strong><br/>
  Session factory code.</p>
  
  <pre><code>private static final SessionFactory sessionFactory;
  private static final Configuration configuration = new Configuration().configure();
  
  static {
      try {
          // Create the SessionFactory from standard (hibernate.cfg.xml) 
          // config file.
          sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
      } catch (Throwable ex) {
          // Log the exception. 
          System.err.println("Initial SessionFactory creation failed." + ex);
          throw new ExceptionInInitializerError(ex);
      }
  }
  
  public static SessionFactory getSessionFactory() {
      return sessionFactory;
  }
  </code></pre>