title: Binding from Set to <form:select> in SpringMVC
body: <p>I am creating web using Spring MVC and I am still beginner with Spring. I have two entities with many to many relationship. in practice, I have UserGroup, which can have many or one (or none) UserAuthority I want to edit these relationships using form with multiple select. Creating new UserGroup entity or editing existing one works fine, with one small issue: when editing existing UserGroup, there are not select appropriate authorities in the select box. I've been working on this for more than a day with no success. Can someone please help me?</p>
  
  <p>My model for entities is:</p>
  
  <pre><code>@Entity
  public class UserGroup {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      private String title;
  
      @ManyToMany(fetch = FetchType.EAGER)
      @JoinTable(
          name = "user_group_has_user_authority",
          joinColumns = {
              @JoinColumn(name = "user_group_id")},
          inverseJoinColumns = {
              @JoinColumn(name = "user_authority_id")})
      private Set&lt;UserAuthority&gt; authorities;
  }
  </code></pre>
  
  <p>(I left out getters and setters, but I have them in my code)</p>
  
  <p>and</p>
  
  <pre><code>public class UserAuthority {
      @Id
      @GeneratedValue(strategy= GenerationType.IDENTITY)
      private Long id;
  
      private String code;
  
      private String name;
  }
  </code></pre>
  
  <p>(again, I left out setters and getters).</p>
  
  <p>My form looks like this:</p>
  
  <pre><code>&lt;form:form action="${action}" method="post" commandName="userGroup" id="user-group-form"&gt;
      &lt;form:input readonly="true" cssClass="dn" path="id"/&gt;
      &lt;table&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;form:label path="title"&gt;Title&lt;/form:label&gt;
                  &lt;form:errors path="title" /&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;form:input path="title"/&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;form:label path="authorities"&gt;Authorities&lt;/form:label&gt;
                  &lt;form:errors path="authorities" /&gt;    
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;form:select path="authorities" multiple="true" items="${authoritiesList}" /&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td colspan="2"&gt;
                  &lt;input type="submit" value="Save" /&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  &lt;/form:form&gt;
  </code></pre>
  
  <p>My contoller methods are following:</p>
  
  <pre><code>@RequestMapping(value = "/editGroup", method = RequestMethod.GET) 
  public ModelAndView editGroup(Model model, HttpServletRequest request) {
      Long id = null;
      UserGroup userGroup = null;
      HashMap&lt;String, Object&gt; editModel = new HashMap&lt;&gt;(2);
  
      try {
          id = Long.parseLong(request.getParameter("id"));
      } catch (NumberFormatException ex) {
      }
      if (id != null) {
          userGroup = groupsManager.get(id);
      } 
  
      UserGroupData userGroupData = groupsManager.createFromUserGroup(userGroup);
  
      editModel.put("userGroup", userGroupData);
      editModel.put("authoritiesList", getAuthoritiesList());
  
      return new ModelAndView("/users/groupEdit", editModel);
  }
  
  @RequestMapping(value = "/editGroup", method = RequestMethod.POST)
  public String updateGroupAction(@ModelAttribute("userGroup") @Validated UserGroupData userGroupData, BindingResult result, Model model, HttpServletRequest request) {
  
      if (result.hasErrors()) {
          model.addAttribute("userGroup", userGroupData);
          return "/users/groupEdit";
      }
      groupsManager.saveUserGroupData(userGroupData);
      return "redirect:/users/listGroup";
  }
  
  private Map &lt;Long, String&gt; getAuthoritiesList() {      //I also tried returning Map&lt;String,String&gt; but result was same.
      Map&lt;Long,String&gt; authorities = new LinkedHashMap&lt;&gt;();
      for (UserAuthority ua : authorityManager.getAll()) {
          authorities.put(ua.getId(), ua.getName());
      }
      return authorities;
  }
  </code></pre>
  
  <p>Finnaly, I use this converter to create UserAuthority from String (actually I googled this somewhere, but it works :) )
      public class StringToUserAuthorityConverter implements Converter {</p>
  
  <pre><code>    /**
       * The string that represents null.
       */
      private static final String NULL_REPRESENTATION = "null";
  
      @Autowired
      private UserAuthorityDao authorityDao;
  
      @Override
      public UserAuthority convert(final String id) {
          if (id.equals(NULL_REPRESENTATION)) {
              return null;
          }
          Long lId = null;
          try {
              lId = new Long(id);
          } catch (NumberFormatException e) {
  
          }
          return authorityDao.find(lId);
      }
  }
  </code></pre>
  
  <p>As I said, all creating and editing works, only problem I have is, that authorities already associated to group are not selected when editing existing group. Any help would be greatly appreciated. </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I found out two things, maybe it can help:</p>
  
  <p>1) In html page, there is following field:</p>
  
  <pre><code>&lt;input type="hidden" value="1" name="_authorities"&gt;
  </code></pre>
  
  <p>I read it should contains values of my authorities field form model, but no matter what, it always contains 1 in my case</p>
  
  <p>2) If my form fails validation (that means if I don't enter Title, which is mandatory) form reloads with error message, but select box is empty. Isn't it sign of something?</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>I resolved point 2) from my last edit...</p>