title: Binding child object on submit spring mvc
body: <p>I'm newbie in Java so this question looks so simple. I have a model like:</p>
  
  <pre><code>@Entity(name="website")
  public class Website {
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  @Column(name="websiteId", nullable=false, unique=true)
  private long websiteId;
  @ManyToOne
  @JoinColumn(name = "publisherId")
  private Publisher publisher;
  
  public Website() {
  }
  
  //... all getter and setter....
  }
  </code></pre>
  
  <p>You see, inside Website class, i have an object of Publisher type:</p>
  
  <pre><code>@Entity(name="publisher")
  public class Publisher {
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  @Column(name="publisherId", nullable=false, unique=true)
  private long publisherId;
  private String publisherName;
  
  @OneToMany(fetch=FetchType.EAGER, cascade = CascadeType.ALL)
  @JoinColumn(name="publisherId")
  private List&lt;Website&gt; listWebsite;
  
  public Publisher() {
  }
  
  //...all getter and setter...
  }
  </code></pre>
  
  <p>OK, now i have a form to submit whole object of Website type. I just created a dropdownlist and let user select publisher:
  The form:</p>
  
  <pre><code>&lt;form:form action="/LineJavaTest1/website/add" commandName="websiteForm" method="post"&gt;
                  &lt;form:input path="websiteName" size="30" placeholder="Website Name"/&gt;
                  &lt;form:input path="websiteUrl" size="30" placeholder="Website Url"/&gt;
                  &lt;form:select path="publisher" multiple="false" size="1"&gt;
                      &lt;%--&lt;form:options itemValue="publisherId" itemLabel="publisherName"/&gt;--%&gt;
                      &lt;form:option value="NONE" label="--- Select ---" /&gt;
                      &lt;form:options items="${publishers}" itemValue="publisherId" itemLabel="publisherName"/&gt;
                  &lt;/form:select&gt;
                  &lt;form:hidden path="websiteId" size="30" placeholder="Website Id"/&gt;
                  &lt;input type="submit" class="btn btn-default" value="Save" /&gt;
              &lt;/form:form&gt;
  </code></pre>
  
  <p>You see, i set the path for the form:select tag as "publisher", the itemValue is set to "publisherId". When posting, it post the publisherId (a long type value) to the publisher property of posted object. The validation will be failed because it required Publisher type, not long type.</p>
  
  <p>My question: How do i posting the Website.Publisher.publisherId in the right way?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>My add actions in controller:</p>
  
  <pre><code>@RequestMapping(value = "/add", method = RequestMethod.GET)
  public String showForm(ModelMap mm, @ModelAttribute("websiteForm") Website websiteForm) {
      mm.put("websiteForm", new Website());
      mm.put("publishers", publisherService.getAll());
      return "website/add";
  }
  
  @RequestMapping(value = "/add", method = RequestMethod.POST)
  public String add(@ModelAttribute("websiteForm") Website websiteForm, BindingResult result, ModelMap mm) {
      websiteValidator.validate(websiteForm, result);
      if (result.hasErrors()) {
          return "error";
      }
  
      if (websiteForm.getWebsiteId()&gt;0) {
          websiteService.edit(websiteForm);
      }else
      {
          websiteService.add(websiteForm);
      }
      return "redirect:index";
  }
  </code></pre>
  
  <p>I have changed the path of select tag from <code>publisher</code> to <code>publisher.publisherId</code> as below suggest from Shantaram Tupe but the form cannot be generated to html. I changed from <code>publisher</code> to <code>publisher.publisherName</code> and everything looks well, the form can be viewed in html, the value post back to server in <code>publisher.publisherName</code>. How can i just post it back in <code>publisher.publisherId</code>? It looks something's wrong in configuration for my publisherId field?</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>My issue is generating HTML form only. I have tried editing the generated html form on browser from something like:</p>
  
  <pre><code>&lt;form.....&gt;
  &lt;select id="publisher.publisherName" name="publisher.publisherName" size="1"&gt;......&lt;/select&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>to something like:</p>
  
  <pre><code>&lt;form.....&gt;
  &lt;select id="publisher.publisherId" name="publisher.publisherId" size="1"&gt;......&lt;/select&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>And everything work well. Now, How can i generate the form with <code>publisher.publisherId</code>?</p>
  
  <p><strong>LAST UPDATE</strong></p>
  
  <p>Finally, i found it. It works with PublisherName but not PublisherId because PublisherName is string type. The dump item in select tag i used (<code>&lt;form:option value="NONE" label="--- Select ---" /&gt;</code>) has the value <code>NONE</code> - that cannot be a long type value. Changed it to <code>0</code> and the form generated successfully.</p>