title: Spring Forms Bind Exception - IllegalStateException: Cannot convert value of type [java.lang.String] to required type
body: <p>I'm newer to Spring and Java and working on getting a form to work in Spring MVC, using JSP, JSTL, Hibernate/JPA and a model class. I can get the page to display fine, but the post that is supposed to create the user is giving me an error. </p>
  
  <pre><code>Failed to convert property value of type java.lang.String to required type com.everesttech.recruiting.searchtool.entity.Role for property user.role; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.everesttech.recruiting.searchtool.entity.Role] for property role: no matching editors or conversion strategy found
  </code></pre>
  
  <p>This error makes sense because the form is passing back an ID for the Role while the User object expects a Role object to be passed. However I'm not sure how to work around this, while still allowing my JSP to be backed by my actual Entity objects as I want to use Spring/Hibernate validations. Is there just a way to declare that form:select as a Role object? Any advice or solutions are much appreciated. Thanks in advanced.</p>
  
  <p>I have provided the relevant sections of code from my project. I did not include code from my service layer and entity layer since I know they are working. However I can include those as well if needed. User has a many to one relationship with Role. </p>
  
  <p>UserController</p>
  
  <pre><code>/**
   * display the user add page
   * 
   * @return              returns the view name
   */
  @RequestMapping(value="/user/add", method=RequestMethod.GET)
  public ModelAndView showUserAdd(ServletRequest request, @ModelAttribute("userModel") UserModel userModel, ModelMap modelmap) {  
  
      ModelMap modelMap = new ModelMap();
      userModel.setUser(new User());
      userModel.setRoles(userService.getRoles());
      modelMap.addAttribute("userModel", userModel);
  
      return new ModelAndView("user/add", modelMap);
  }
  
  /**
   * handle post request to handle user creation, if successful displays user list page
   * 
   * @param user          User object from JSP
   * @param result        BindingResult from JSP
   * @return              returns view name
   */
  @RequestMapping(value="/user/add", method=RequestMethod.POST)
  public String userAdd(UserModel userModel, BindingResult result) {
  
      if (result.hasErrors()) {
          logger.warn("Error binding UserModel for /user/add, returning to previous page");
          List&lt;ObjectError&gt; errors = result.getAllErrors();
          for (ObjectError error : errors) {
              logger.warn(error.getCode() + " - " + error.getDefaultMessage());
          }
          return "user/add";
      }
  
      try {
          User user = userModel.getUser();
          userService.addUser(user);
      }
      catch (DuplicateKeyException e) {
          logger.warn("Duplicate record found in User table", e);
      }
      catch (Exception e) {
          logger.error("Error occurred while trying to create a new user", e);
      }
  
      return "/user";
  }
  </code></pre>
  
  <p>This is the model I am passing to the JSP witch contains a User object, which is the Hibernate Entity, and a list of Roles, another hibernate Entity.</p>
  
  <pre><code>import java.util.List;
  
  import org.springframework.context.annotation.Scope;
  import org.springframework.stereotype.Component;
  
  import com.everesttech.recruiting.searchtool.entity.Role;
  import com.everesttech.recruiting.searchtool.entity.User;
  
  @Scope(value="request")
  @Component("userModel")
  public class UserModel {
  
      public User user;
      public List&lt;Role&gt; roles;
  
      public UserModel() {
  
      }
  
      public UserModel(User user, List&lt;Role&gt; roles) {
          this.user = user;
          this.roles = roles;
      }
  
      public User getUser() {
          return user;
      }
  
      public void setUser(User user) {
          this.user = user;
      }
  
      public List&lt;Role&gt; getRoles() {
          return roles;
      }
  
      public void setRoles(List&lt;Role&gt; roles) {
          this.roles = roles;
      }
  
      @Override
      public String toString() {
          return "UserModel [user=" + user + ", roles=" + roles + "]";
      }
  }
  </code></pre>
  
  <p>Here is my JSP.</p>
  
  <pre><code>&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&gt;
  &lt;%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form" %&gt;
  &lt;%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %&gt;
  
  &lt;div class="container"&gt;
      &lt;div class="row"&gt;
          &lt;div class="col-xs-12 col-sm-8 col-md-6"&gt;
              &lt;h3&gt;User - Add&lt;/h3&gt;
              &lt;br&gt;
  
              &lt;spring:hasBindErrors name="userModel"&gt;
                  &lt;div class="alert alert-danger"&gt;
                      &lt;sf:errors path="userModel.user.firstName"&gt;&lt;/sf:errors&gt;
                      &lt;sf:errors path="userModel.user.lastName"&gt;&lt;/sf:errors&gt;
                      &lt;sf:errors path="userModel.user.email"&gt;&lt;/sf:errors&gt;
                      &lt;sf:errors path="userModel.user.userName"&gt;&lt;/sf:errors&gt;
                      &lt;sf:errors path="userModel.user.password"&gt;&lt;/sf:errors&gt;
                      &lt;sf:errors path="userModel.user.role"&gt;&lt;/sf:errors&gt;
                  &lt;/div&gt;
              &lt;/spring:hasBindErrors&gt;
  
              &lt;c:url var="formAction" value="/user/add" /&gt;
  
              &lt;sf:form commandName="userModel" modelAttribute="userModel" method="post" action="${formAction}"&gt; 
                  &lt;div class="form-group"&gt;
                      &lt;label for="first-name"&gt;First Name&lt;/label&gt;
                      &lt;sf:input path="user.firstName" id="first-name" class="form-control" placeholder="First Name" /&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label for="last-name"&gt;Last Name&lt;/label&gt;
                      &lt;sf:input path="user.lastName" id="last-name" class="form-control" placeholder="Last Name" /&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label for="email"&gt;Email&lt;/label&gt;
                      &lt;sf:input path="user.email" id="email" class="form-control" placeholder="Email" /&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label for="user-name"&gt;Username&lt;/label&gt;
                      &lt;sf:input path="user.userName" id="user-name" class="form-control" placeholder="Username" /&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label for="password"&gt;Password&lt;/label&gt;
                      &lt;sf:password path="user.password" id="password" class="form-control" placeholder="" /&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label for="confirm-password"&gt;Confirm Password&lt;/label&gt;
                      &lt;input type="password" id="confirm-password" class="form-control" placeholder="" /&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label for="role"&gt;Role&lt;/label&gt;
                      &lt;sf:select path="user.role" id="role" class="form-control" &gt;
                          &lt;c:forEach var="r" items="${userModel.roles}"&gt;
                              &lt;sf:option value="${r.getId()}"&gt;${r.getFriendlyName()}&lt;/sf:option&gt;
                          &lt;/c:forEach&gt;
                      &lt;/sf:select&gt;
                  &lt;/div&gt;
                  &lt;button type="submit" class="btn btn-default"&gt;Save&lt;/button&gt;
                  &lt;button type="button" class="btn btn-default"&gt;Cancel&lt;/button&gt;
              &lt;/sf:form&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>