title: Using converter with spring mvc
body: <p>so I'm new in using spring mvc , for the fist tutorial I followed the example of <a href="http://websystique.com/springmvc/spring-mvc-4-and-spring-security-4-integration-example/" rel="nofollow noreferrer">http://websystique.com/springmvc/spring-mvc-4-and-spring-security-4-integration-example/</a> tutorial . So we have a datamodel called User and another datamodel userProfile , we have a relation between the user and userProfile , every user have one or many userProfile , here is the class </p>
  
  <p><strong>User.java</strong></p>
  
  <pre><code>@Entity
  @Table(name="APP_USER")
  public class User implements Serializable{
  ......
  
  @JsonManagedReference
  @NotEmpty
  @ManyToMany(fetch = FetchType.EAGER)
  @JoinTable(name = "APP_USER_USER_PROFILE", 
           joinColumns = { @JoinColumn(name = "USER_ID") }, 
           inverseJoinColumns = { @JoinColumn(name = "USER_PROFILE_ID") }) 
  private Set&lt;UserProfile&gt; userProfiles = new HashSet&lt;UserProfile&gt;(); 
  
  @JsonBackReference
  @OneToMany(mappedBy="user",cascade=CascadeType.ALL,fetch=FetchType.EAGER)
  private Set&lt;Car&gt; cars = new HashSet&lt;Car&gt;();
  </code></pre>
  
  <p>So , to add a new user with userProfile we have to add a converter for userProfile </p>
  
  <p><strong>RoleToUserProfileConverter.java</strong></p>
  
  <pre><code>@Component
  public class RoleToUserProfileConverter implements Converter&lt;Object, UserProfile&gt;{
  
  static final Logger logger = LoggerFactory.getLogger(RoleToUserProfileConverter.class);
  
  @Autowired
  UserProfileService userProfileService;
  
  /**
   * Gets UserProfile by Id
   * @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
   */
  public UserProfile convert(Object element) {
      Integer id = Integer.parseInt((String)element);
      UserProfile profile= userProfileService.findById(id);
      logger.info("Profile : {}",profile);
      return profile;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The form works fine and I can add a new user without any errors , after that I created another relation , I added a new class Car.java in relation with User.java , </p>
  
  <p><strong>Car.java</strong></p>
  
  <pre><code>public class Car implements Serializable {
  .........
  
  @JsonManagedReference
  @NotNull
  @ManyToOne(optional=false)
  @JoinColumn(name="user_fk")
  private User user;
  
  }
  </code></pre>
  
  <p>I added a converter for the user class </p>
  
  <p><strong>StringToUser.java</strong></p>
  
  <pre><code>@Component
  public class StringtoUser implements Converter&lt;Object, User&gt;{
  
  @Autowired
  UserService userService ;
  
  @Override
  public User convert(Object element) {
      if (element == null ) {
          return null;
      }
          Integer id = ((User)element).getId();
          User user = userService.findById(id);
          return user;
  }
  </code></pre>
  
  <p>After adding this converter I get an error in user form and Car form works fine :</p>
  
  <p><strong>Error :</strong></p>
  
  <pre><code>WARNING: Failed to bind request element: org.springframework.beans.TypeMismatchException: Failed to convert value of type [com.websystique.springmvc.model.User] to required type [com.websystique.springmvc.model.User]; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [com.websystique.springmvc.model.User] to type [@javax.validation.Valid com.websystique.springmvc.model.User] for value 'User [id=null, ssoId=c, password=c, firstName=c, lastName=c, email=c]'; nested exception is java.lang.NullPointerException
  </code></pre>
  
  <p>When I removed the User Converter , the user form works fine and in the car form I get an error :</p>
  
  <p><strong>Error :</strong> </p>
  
  <pre><code>org.apache.jasper.JasperException: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute
  </code></pre>
  
  <p>how can it be done please !!!!!!</p>