How to data bind to CheckBox using Many to many annotation in Spring MVC
<p>I want to do that;</p>

<p>I have got a work to improve CRUD applications for learning Spring, Hibernate, and JPA. So I want to do Many to Many example but I ask someone to do this.</p>

<p>I want to get employee's driver licenses and update info. I can get employee's info from database to CheckBox in web page using Spring forEach. If employee has which driver license, relational CheckBoxes must be checked. I can update this information.</p>

<p>For example, employee has got driver licenses of "A1" and "M" class. I can get and bind data.</p>

<p>I created tables of "employees", "driving_licenses". 
I created tables of "employee_driving_licenses" for store employee's driver licenses:</p>

<p>employees:</p>

<pre><code>id          int
last_name   nvarchar(20)
first_name  nvarchar(10)
</code></pre>

<p>driving_licenses:</p>

<pre><code>id      int 
code    nvarchar(5)
</code></pre>

<p>employee_driving_licenses:</p>

<pre><code>employee_id         int
driving_license_id  int
</code></pre>

<p>I created table's model class and added JPA annotations. I have got Employee and DrivingLicense  Entity Java class:</p>

<p>Employee.java</p>

<pre><code>@Entity
@Table(name = "employees")
public class Employee {

    private int id;
    private String last_name;
    private String first_name;
    private java.util.Collection&lt;DrivingLicense&gt; driving_licenses = new HashSet&lt;DrivingLicense&gt;();

    @GeneratedValue(strategy = GenerationType.AUTO)
    @Id
    @Column
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @NotEmpty(message = "Please enter Last Name")
    @Column
    public String getLast_name() {
        return last_name;
    }

    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }

    @NotEmpty(message = "Please enter First Name")
    @Column
    public String getFirst_name() {
        return first_name;
    }

    @ManyToMany(fetch = FetchType.EAGER, mappedBy = "employees")
    public java.util.Collection&lt;DrivingLicense&gt; getDriving_licenses() {
        return driving_licenses;
    }

    public void setDriving_licenses(
        java.util.Collection&lt;DrivingLicense&gt; driving_licenses) {this.driving_licenses = driving_licenses;
    }
}
</code></pre>

<p>DrivingLicense.java</p>

<pre><code>@Entity
@Table(name = "driving_licenses")
public class DrivingLicense {

    private int id;
    private String code;
    private java.util.Collection&lt;Employee&gt; employees = new HashSet&lt;Employee&gt;();

    @Override
    public int hashCode() {
        return new Long(id).hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
                return false;
        }
        if (!(obj instanceof DrivingLicense)) {
                return false;
        }
        return this.id == ((DrivingLicense) obj).getId();
    }

    @GeneratedValue(strategy = GenerationType.AUTO)
    @Id
    @Column
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column
    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @ManyToMany(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
    @JoinTable(name = "employee_driving_licenses", joinColumns = { @JoinColumn(name = "driving_license_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "employee_id", nullable = false, updatable = false) })
    public java.util.Collection&lt;Employee&gt; getEmployees() {
        return employees;
    }

    public void setEmployees(java.util.Collection&lt;Employee&gt; employees) {
        this.employees = employees;
    }
}
</code></pre>

<p>I created application DAO, Service, Controller and View classes/pages.</p>

<p>EmployeeController.java:</p>

<pre><code>@RequestMapping(value = "index", method = RequestMethod.POST)
public String employee(@Valid @ModelAttribute("employee") Employee employee,
    BindingResult result, @RequestParam String action,
    java.util.Map&lt;String, Object&gt; map) {

    boolean isValid = true;
    if (result.hasErrors()) {
        isValid = false;
    }

    Employee employeeResult = new Employee();
    switch (action.toLowerCase()) {
        case "add":
            if (isValid) 
                employeeService.add(employee);
            employeeResult = employee;
        break;

        case "edit":
            if (isValid) 
               employeeService.edit(employee);
            employeeResult = employee;
        break;
    }
    map.put("drivingLicenseList", drivingLicenseService.findActiveDrivingLicense());
    map.put("employee", employeeResult);
    map.put("activeEmployeeList", employeeService.findActiveEmployees());
    return "employee";
}
</code></pre>

<p>employee.jsp:</p>

<pre><code>&lt;c:forEach var="drive_licenses" items="${drivingLicenseList}"&gt;
    &lt;form:checkbox path="driving_licenses"
    value="${drive_licenses }" label="${drive_licenses.code }" /&gt;
&lt;/c:forEach&gt;
</code></pre>

<p>When I want to update employee's driver licenses, I am receiving an error code as below:</p>

<pre><code>{employee=com.ay.model.Employee@573cb29c, org.springframework.validation.BindingResult.employee=org.springframework.validation.BeanPropertyBindingResult:     1 errors
Field error in object 'employee' on field 'driving_licenses': rejected value      [com.ay.model.DrivingLicense@1,com.ay.model.DrivingLicense@7,com.ay.model.DrivingLicense@8,com.ay.model.Driving    License@9]; codes     [typeMismatch.employee.driving_licenses,typeMismatch.driving_licenses,typeMismatch.java.util.Collection,typeMis    match]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes     [employee.driving_licenses,driving_licenses]; arguments []; default message [driving_licenses]]; default     message [Failed to convert property value of type 'java.lang.String[]' to required type 'java.util.Collection'     for property 'driving_licenses'; nested exception is java.lang.IllegalStateException: Cannot convert value of     type [java.lang.String] to required type [com.ay.model.DrivingLicense] for property 'driving_licenses[0]': no     matching editors or conversion strategy found]}
</code></pre>

<p>How can I get values with type of Collection? Please give advice to me.</p>

<p>And also, If I add this code </p>

<pre><code>value="${drive_licenses }" 
</code></pre>

<p>on the web page of employee.jsp, I can see object value  on browser normally. Actually firstly I added this code </p>

<pre><code>value="${drive_licenses.id }"
</code></pre>

<p>But employees's driver licenses were not checked on the CheckBoxes and every CheckBoxes values was got empty. What is the true syntax?</p>

<p>Thank you for supporting.</p>