title: Spring form select a enum from a object
body: <p>I'm making a form to edit users, each user has multiples roles (based on Spring Security). The problem is that the select option is not preselected with the correspondents roles of the users.</p>
  
  <p>User</p>
  
  <pre><code>public class User implements UserDetails {
     ...
     private Set&lt;UserRole&gt; userRoles = new HashSet&lt;&gt;(0);
     ...
  }
  </code></pre>
  
  <p>Roles</p>
  
  <pre><code>public class UserRole implements GrantedAuthority {
     ...
     private Role role; //enum {ROLE_ADMIN, ROLE_USER,...}
     ...
  }
  </code></pre>
  
  <p>Form</p>
  
  <pre><code>&lt;form:fomr commandName="user"&gt;
     &lt;form:input path="username" /&gt;
     ...
     &lt;form:select multiple="true" path="userRoles" itemValue="role"&gt;
        &lt;form:options items="${roles}"/&gt;
     &lt;/form:select&gt;
     ...
  &lt;/form:form&gt;
  </code></pre>
  
  <p>I'm sure that the problem come from "userRoles", it is a <code>Set</code> of a different type that "roles". There is an another way to make the form?</p>
  
  <p>PS:<code>items=${roles} == Roles.values()</code></p>
  
  <p><strong>edited:</strong></p>
  
  <pre><code>User user = getUserById(id);
  model.addAttribute("newUser", user);
  
  @Transactional
  public User getUserById(int id) {
      Session session = sessionFactory.getCurrentSession();
      User user = (User) session.get(User.class, id);
      if (user != null) {
          user.getUserRoles().size();
      }
      return user;
  }
  </code></pre>