title: Spring MVC checkbox tag bound to collection expects object but validation expects object.id
body: <p>In my Spring MVC project I have an update page for Class1 that must display a list of form:checkbox tags that is bound to a collection of entities on Class1. </p>
  
  <p>Class1.java:</p>
  
  <pre><code>class Class1 {
    private Set&lt;Class2&gt; set;
    //... other fields 
  }
  </code></pre>
  
  <p>In updateclass1.jspx:</p>
  
  <pre><code>&lt;c:forEach items="${allClass2Instances}" var="class2"&gt;
     &lt;form:checkbox label="${class2.name}" path="set" value="${class2}"/&gt;&lt;br/&gt;
  &lt;/c:forEach&gt;
  </code></pre>
  
  <p>With the checkbox tag as above, when I display the page, the checkbox is ticked if the Class2 instance is part of the Set on class1, and unticked if it isn't. But when I hit submit, I get the following error:</p>
  
  <pre><code>Failed to convert property value of type 'java.lang.String[]' to required type 'java.util.Set' for property 'set'; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "Name 1" from type 'java.lang.String' to type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "Name1"
  </code></pre>
  
  <p>As far as I can tell when the page is populated, the form:checkbox tag needs an instance to set the correct checked/unchecked value, but on submit the JSP is sending an array of class2.toString() values to a converter that expects the IDs. Conversely when I change the tag to the following:</p>
  
  <pre><code>&lt;form:checkbox label="${class2.name}" path="set" value="${class2.id}"/&gt;&lt;br/&gt;
  </code></pre>
  
  <p>The binding works fine, but when I view the update page the checkboxes are not ticked / unticked correctly because the tag does not know that value being passed in is the object id.</p>
  
  <p>How do I make the binding after submit consistent with what the checkbox tag expects?</p>
  
  <p>In case it matters - this is all inside a scaffolding page generated by Roo. </p>