title: Spring MVC - Child entity lost after submit
body: <p>I'm going to try to explain my problem as completely and shortly as I can...</p>
  
  <p>A web application, made on Spring MVC 2.5 + Hibernate + Java 6 (not using annotation!).</p>
  
  <p>I've got a controller extending SimpleFormController and a jsp page that is its formView and successView.</p>
  
  <p>This controller should help me to insert into db an entity PracticeT that has connected (many to one) a lookup entity PracticeConfT (think about it as a "typology"). I need to choose that "typology" through a drop-down menu. In my webapp I need to be able to save data inserted and when I want, to submit the request for approval.</p>
  
  <p>The page has some text fields and that drop-down menu. The bean called as default "command" is NewPracticeBean that has within a reference to an object PracticeT.</p>
  
  <p>THE PROBLEM IS: I fill the form, I select a typology from the drop-down menu, I submit form and save data on DB but when I come back to the view, every property is there but the drop-down menu it is not: it has all the options allowed but no one selected. Some checks revealed that the entity PracticeConfT is null (but it has been recorded on db correctly and debugging it is still there in the model until the very end of the method onSubmit!!!).</p>
  
  <p>I hope someone can help me. Thank you in advance!
  Bye,
  Dolfiz</p>
  
  <p>Here some useful code:
  (I don't think that hibernate config can be the problem, but if you need it, I can post it too)</p>
  
  <p><strong>newPractice.jsp</strong></p>
  
  <pre><code>&lt;form:form id="newPracticeForm" commandName="command"&gt;
      &lt;input type="hidden" name="action"/&gt;
      &lt;spring:nestedPath path="practiceT"&gt;
          &lt;table class="table-data-form"&gt;
              &lt;tr&gt;
                  &lt;td class="left"&gt;&lt;spring:message code="" text="Practice type" /&gt;&lt;/td&gt;
                  &lt;td&gt;
                      &lt;form:select path="practiceConfT" multiple="false"&gt;
                          &lt;form:option value="" label="- seleziona -"/&gt;
                          &lt;form:options items="${practiceTypeList}" itemValue="idPracticeConf" itemLabel="practiceName"/&gt; 
                      &lt;/form:select&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td class="left"&gt;
                      &lt;spring:message code="" text="Opzione divisa" /&gt;
                      &lt;br/&gt;&lt;form:errors cssClass="errors" path="opzioneDivisa" /&gt;
                  &lt;/td&gt;
                  &lt;td&gt;&lt;form:input path="opzioneDivisa" /&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td colspan="1"&gt;
                      &lt;input type="submit" name="submit" id="submit" value="Save" class="buttonEMS" style="width:100px;" /&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
          &lt;/table&gt;
      &lt;/spring:nestedPath&gt;
  &lt;/form:form&gt;
  </code></pre>
  
  <p><strong>NewPracticeBean.java</strong></p>
  
  <pre><code>public class NewPracticeBean implements Serializable{
  
      private PracticeT practiceT;
      private String action;
      private boolean typeSelected;
  
      public NewPracticeBean(){
          super();
          this.practiceT = new PracticeT();
      }
  
      // getters &amp; setters...
  }
  </code></pre>
  
  <p><strong>PracticeT.java</strong></p>
  
  <pre><code>public class PracticeT implements java.io.Serializable {
  
      private long idPractice;
      private PracticeConfT practiceConfT;
      private String opzioneDivisa;
  
      // getters &amp; setters...
  }
  </code></pre>
  
  <p><strong>PracticeConfT.java</strong></p>
  
  <pre><code>public class PracticeConfT implements java.io.Serializable {
  
      public static final String PRACTICE_NAME = "practiceName";
  
      private long idPracticeConf;
      private String practiceName;
  
      // getters &amp; setters...
  }
  </code></pre>
  
  <p><strong>NewPracticeController.java</strong>
      public class NewPracticeController extends SimpleFormController{</p>
  
  <pre><code>    protected SmartLogger log = SmartLogger.getLogger(this.getClass());
  
      private PracticeSu practiceSu;
      private ConfigurationSu configurationSu;
  
      private HibernateEntityDataBinder practiceConfTBinder;
      private HibernateEntityDataBinder practiceTBinder;
  
      public NewPracticeController() {
          setCommandClass(NewPracticeBean.class);
          setCommandName("command");
      }
  
      @Override
      protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
          log.trace("NewPracticeController -- initBinder");
          super.initBinder(request, binder);
          binder.registerCustomEditor(PracticeT.class, "practiceT", practiceTBinder);
          binder.registerCustomEditor(PracticeConfT.class, "practiceT.practiceConfT", practiceConfTBinder);
      }
  
      @Override
      protected Map referenceData(HttpServletRequest request) throws Exception {
          log.trace("NewPracticeController -- referenceData");
          Map model = new HashMap();
          RetrieveAllEntitiesReq req = new RetrieveAllEntitiesReq();
          req.setEntity(PracticeConfT.class);
          req.setOrderProperty(PracticeConfT.PRACTICE_NAME);
          RetrieveAllEntitiesResp resp = configurationSu.retrieveAllEntities(req);
          List entitiesList = resp.getEntitiesList();
          model.put("practiceTypeList", entitiesList);
  
          return model;
      }
  
      @Override
      protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
          NewPracticeBean practiceBean = (NewPracticeBean)command;
          Map model = errors.getModel();
  
          CreateNewPracticeReq req = new CreateNewPracticeReq();
          req.setPracticeT(practiceBean.getPracticeT());
          CreateNewPracticeResp resp = practiceSu.createNewPractice(req);
          practiceBean.setPracticeT(resp.getPracticeT());
  
          model.putAll(referenceData(null));
          model.put(getCommandName(), practiceBean);
  
          return new ModelAndView(getSuccessView(), model);
      }
  
      // setters and getters...
  }
  </code></pre>