title: spring jdbc template custom type
body: <p>In my project I use Spring 3.0 JdbcTemplate to implement DAO classes. It provides convenient methods like query(...), update(...) etc. These methods accept objects as arguments to bind to the query. In javadoc it is stated that it is left to the PreparedStatement to guess the corresponding SQL type. So when using primitives or wrappers it is simple.</p>
  
  <p>But in my code I use special classes for id representation. For example UserId. It has public method to get its integer value - getInt(). Right now I have to use</p>
  
  <pre><code>userId.getInt()
  </code></pre>
  
  <p>every time I need to pass instance of UserId to the JdbcTemplate query. If I forget and write just</p>
  
  <pre><code>userId
  </code></pre>
  
  <p>I obviously get SQLException, as my UserId object can't be consumed by prepared statement (<a href="http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html#setObject%28int,%20java.lang.Object%29" rel="nofollow">here are the rules to map Object type to corresponding SQL type</a>). But this type of error can't be spotted during compilation (as JdbcTemplate accepts Object as parameter), which makes bugs introducing easy.</p>
  
  <p>Is there any way I can avoid calling .getInt() and just pass my UserId object into the query?</p>