title: Spring JDBC + Postgres SQL + Java 8 - conversion from/to LocalDate
body: <p>I am using Postgres SQL 9.2, Spring JDBC with version 4.0.5, and Java 8. <br>
  Java 8 introduced new date/time API and I would like to use it, but I encountered some difficulties.
  I have created table TABLE_A:</p>
  
  <pre><code>CREATE TABLE "TABLE_A"
  (
    new_date date,
    old_date date
  )
  </code></pre>
  
  <p>I am using Spring JDBC to communicate with database. I have created Java class, which corresponds to this table:</p>
  
  <pre><code>public class TableA
  {
      private LocalDate newDate;
      private Date oldDate;
      //getters and setters
  
  }
  </code></pre>
  
  <p>this is my code which is reponsible for inserting new row:</p>
  
  <pre><code>public void create(TableA tableA)
  {
      BeanPropertySqlParameterSource parameterSource = new BeanPropertySqlParameterSource(tableA);
      final String sql = "INSERT INTO public.TABLE_A (new_date,old_date) values(:newDate,:oldDate)";
      namedJdbcTemplate.update(sql,parameterSource);
  
  }
  </code></pre>
  
  <p>When I executed this method I got exception:</p>
  
  <pre><code>org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of java.time.LocalDate. Use setObject() with an explicit Types value to specify the type to use.
  </code></pre>
  
  <p>so I updated cretion of BeanPropertySqlParameterSource: </p>
  
  <pre><code>BeanPropertySqlParameterSource parameterSource = new BeanPropertySqlParameterSource(tableA);
  parameterSource.registerSqlType("newDate", Types.DATE); 
  </code></pre>
  
  <p>after that change I was able to insert row. But next, I would like to fetch rows from database. Here is my method:</p>
  
  <pre><code>public List&lt;TableA&gt; getAll()
  {
      final String sql = "select * from public.TABLE_A";
      final BeanPropertyRowMapper&lt;TableA&gt; rowMapper = new BeanPropertyRowMapper&lt;TableA&gt;(TableA.class);
      return namedJdbcTemplate.query(sql,rowMapper);
  }
  </code></pre>
  
  <p>and of course I got exception:</p>
  
  <pre><code>...
  at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:474)
  at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:511)
  at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1119)
  at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:902)
  at org.springframework.jdbc.core.BeanPropertyRowMapper.mapRow(BeanPropertyRowMapper.java:255)
  ...
  Caused by: java.lang.IllegalStateException: Cannot convert value of type [java.sql.Date] to required type [java.time.LocalDate] for property 'newDate': no matching editors or conversion strategy found.
  </code></pre>
  
  <p>So I updated my code, this time BeanPropertyRowMapper, I have added conversion service to bean wrapper, which is able to perform conversion from java.sql.Date to java.time.LocalDate</p>
  
  <pre><code>public List&lt;TableA&gt; getAll()
  {
      final String sql = "select * from public.TABLE_A";
      final BeanPropertyRowMapper&lt;TableA&gt; rowMapper = new BeanPropertyRowMapper&lt;TableA&gt;(TableA.class)
      {
          @Override
          protected void initBeanWrapper(BeanWrapper bw) {
              super.initBeanWrapper(bw);
             bw.setConversionService(new ConversionService() {
                 @Override
                 public boolean canConvert(Class&lt;?&gt; aClass, Class&lt;?&gt; aClass2) {
                     return aClass == java.sql.Date.class &amp;&amp; aClass2 == LocalDate.class;
                 }
  
                 @Override
                 public boolean canConvert(TypeDescriptor typeDescriptor, TypeDescriptor typeDescriptor2) {
                     return canConvert(typeDescriptor.getType(), typeDescriptor2.getType());
                 }
  
                 @Override
                 public &lt;T&gt; T convert(Object o, Class&lt;T&gt; tClass) {
                     if(o instanceof Date &amp;&amp; tClass == LocalDate.class)
                     {
                         return (T)((Date)o).toLocalDate();
                     }
  
                     return null;
  
  
         }
  
             @Override
             public Object convert(Object o, TypeDescriptor typeDescriptor, TypeDescriptor typeDescriptor2) {
                 return convert(o,typeDescriptor2.getType());
             }
         });
      }
  }   ;
  
  return namedJdbcTemplate.query(sql,rowMapper);
  </code></pre>
  
  <p>and now everything works, but it is quite complicated.<br> 
  Is it easier way to achieve that?
  Generally speaking, <strong>I would like to operate on LocalDate in my Java code, because it is much more convenient, and be able to persist it to database</strong>. I would expect that it should be enabled by default.</p>