title: Values of @PathVariable and @ModelAttribute overlapping
body: <p>I have an <code>User</code> object stored in the session with <code>@SessionAttributes</code>. And a straight-forward method decorated with <code>@ModelAttribute</code> in order to initialize it whenever the session's value is null.</p>
  
  <p>User class:</p>
  
  <pre><code>@Entity
  @Table( name="USER")
  public class User implements java.io.Serializable {
  
      private Long id;
      private String username;
      private String password;
      ....
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name ="ID")
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
      ...
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>@RequestMapping("/item")
  @Controller
  @SessionAttributes({"user"})
  public class MyController {
  </code></pre>
  
  <p>@ModelAttribute method:</p>
  
  <pre><code>@ModelAttribute("user")
      public User createUser(Principal principal) {
          return userService.findByUsername(principal.getName());
      }
  </code></pre>
  
  <p>It all seems to work as expected except in this particular method:</p>
  
  <pre><code>@RequestMapping(value = "/{id}", method = RequestMethod.GET)
      public String showItem(@PathVariable("id") Long id, @ModelAttribute("user") User user,
              Model uiModel) {
     ...    
  }
  </code></pre>
  
  <p><strong>The problem is that <code>User.id</code> is being set with <code>@PathVariable("id")</code></strong>. I believe I ran into this with <code>@RequestParam</code> too. I'm assuming that's because both have the same name and type. After reading <a href="http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib-method-args" rel="noreferrer">Spring's documentation</a> (see below) I'm assuming this is expected behavior:</p>
  
  <blockquote>
    <p>The next step is data binding. The WebDataBinder class matches request parameter names — including query string parameters and form fields — to model attribute fields by name. Matching fields are populated after type conversion (from String to the target field type) has been applied where necessary. </p>
  </blockquote>
  
  <p>However, I would think this scenario is fairly common, how are other people handling this? If my findings are correct and this is expected behavior (or bug), this seems to be very error prone. </p>
  
  <p><strong>Possible solutions:</strong></p>
  
  <ol>
  <li>Change <code>@PathVariable("id")</code> to <code>@PathVariable("somethingElse")</code>. Works but it's not as straightforward with @RequestParam (e.g. I don't know how to change jqgrid's request parameter id to something else but this is another issue).  </li>
  <li>Change <code>@PathVariable("id")</code> type from Long to Int. This will make <code>User.id</code> and <code>id</code> types differ but the cast to Long looks ugly :)</li>
  <li>Don't use <code>@ModelAttribute</code> here and query the DB for <code>User</code> again. Not consistent with other methods and involves redundant DB calls.</li>
  </ol>
  
  <p>Any suggestions?</p>