title: Spring Boot, Thymeleaf, ManyToMany checkboxes evaluation
body: <p>I've seen a lot of examples on the Internet and looks like the solution should work fine. But still could not make my code working. </p>
  
  <p>User:</p>
  
  <pre><code>@Entity
  @Table(name = "users")
  public class User implements Serializable{
  
  private static final long serialVersionUID = 1L;    
  ...
  
  @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  @JoinTable(name = "user_usertypes", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
          inverseJoinColumns = @JoinColumn(name = "usertype_id", referencedColumnName = "id"))
  private Set&lt;UserType&gt; userTypes;
  }
  </code></pre>
  
  <p>UserType:</p>
  
  <pre><code>@Entity
  @Table(name = "usertypes")
  public class UserType implements Serializable{
  
  private static final long serialVersionUID = 1L;
  
  @Id
  @Column(name = "id")
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Integer id;
  
  @Version
  @Column(name = "version")
  private Integer version;
  
  @Column(name = "name")
  private String name;
  
  @ManyToMany(mappedBy = "userTypes")
  private Set&lt;User&gt; users;
  
  @Override
  public int hashCode() {
      int hash = 5;
      hash = 83 * hash + Objects.hashCode(this.id);
      return hash;
  }
  
  @Override
  public boolean equals(Object obj) {
      System.out.println("comparing objects");
      if (obj == null) {
          return false;
      }
      if (getClass() != obj.getClass()){
          return false;
      }
      final UserType other = (UserType) obj;
      return Objects.equals(this.id, other.id);
  }
  }
  </code></pre>
  
  <p>User Controller:</p>
  
  <pre><code>@Controller
  public class UserController {
      @RequestMapping(value = "/user", method = RequestMethod.POST)
  public String saveUser(@Valid @ModelAttribute("user") User user, BindingResult bindingResult) {
      if (bindingResult.hasErrors()) {
          return "users/userform";
      }
      System.out.println(user.getUserTypes());
      userService.saveUser(user);
      return "redirect:/user/" + user.getId();
  }
  
  @InitBinder
  private void initBinder(ServletRequestDataBinder binder) {
  
      binder.registerCustomEditor(Set.class, "userTypes", new CustomCollectionEditor(Set.class) {
          protected Object convertElement(Object element) {
              if (element != null) {
                  System.out.println("From Controller: " + element.toString());
                  return userTypeService.findOne(Integer.parseInt(element.toString()));
              }
              return null;
          }
  
      });
  }
  </code></pre>
  
  <p>userform:</p>
  
  <pre><code>&lt;form th:object="${user}" th:action="@{/user}" method="post"&gt;
  &lt;input type="hidden" th:field="*{id}"/&gt;
  &lt;ul&gt;
     &lt;li th:each="type : ${types}"&gt;
       &lt;input type="checkbox"  th:id="${type.id}" th:field="*{userTypes}" th:value="${type.id}"/&gt;
       &lt;label th:for="${type.id}" th:text="${type.name}"&gt;name&lt;/label&gt;
     &lt;/li&gt;
   &lt;/ul&gt;
  &lt;form&gt;
  </code></pre>
  
  <p>The initBinder isn't called on submit. Only on page load.
  So, my controller cannot get the userTypes objects. What is missing? Thank you!</p>