title: JPA 2.1: Introducing Java 8 Date/Time API
body: <p><strong>I'd like to add support</strong> for the <strong>Java 8 Date/Time API (JSR-310)</strong> in my JPA-enabled application.<br></p>
  
  <p>It's clear that <strong>JPA 2.1</strong> <a href="https://stackoverflow.com/a/23718471/324152">does not support the Java 8 Date/Time API</a>.<br>
  As a workaround, the most common advise is to use an <a href="http://www.thoughts-on-java.org/persist-localdate-localdatetime-jpa/" rel="nofollow noreferrer"><code>AttributeConverter</code></a>.</p>
  
  <p>In my existing application, I changed my entities to use <code>LocalDate</code>/<code>LocalDateTime</code> types for the column mapping fields and added legacy setter/getters for <code>java.util.Date</code> to them.<br>
  I created corresponding <code>AttributeConverter</code> classes.</p>
  
  <p><strong>My application does now fail</strong> when using <code>Query.setParameter()</code> with <code>java.util.Date</code> instances (it worked before the transition to the new API). <br>It seems that JPA expects the new date types and does not convert it on the fly.<br><br>
  <strong>I expected that if passing an argument</strong> to <code>setParameter()</code> of a type for which an <code>AttributeConverter</code> has been registered, it <strong>would be automatically converted</strong> by the converter.
  But this seems to be not the case, at least not using <code>EclipseLink 2.6.2</code>:</p>
  
  <pre><code>java.lang.IllegalArgumentException: You have attempted to set a value of type class java.util.Date for parameter closeDate with expected type of class java.time.LocalDate from query string SELECT obj FROM [...]
      at org.eclipse.persistence.internal.jpa.QueryImpl.setParameterInternal(QueryImpl.java:937) ~[eclipselink-2.6.2.jar:2.6.2.v20151217-774c696]
      at org.eclipse.persistence.internal.jpa.EJBQueryImpl.setParameter(EJBQueryImpl.java:593) ~[eclipselink-2.6.2.jar:2.6.2.v20151217-774c696]
      at org.eclipse.persistence.internal.jpa.EJBQueryImpl.setParameter(EJBQueryImpl.java:1) ~[eclipselink-2.6.2.jar:2.6.2.v20151217-774c696]
    [...]
  </code></pre>
  
  <p><br></p>
  
  <p><strong><em>Questions</em></strong>:</p>
  
  <ol>
  <li>Is this behavior <strong>expected</strong>? Did I miss something?</li>
  <li>Is there a <strong>way</strong> to use the new date types as fields <strong>without breaking existing code</strong>?</li>
  <li><strong>How did you deal</strong> with the transition to the new Date/Time API within JPA?</li>
  </ol>
  
  <p><br>
  <strong><em>UPDATE:</em></strong> <br><br>
  However, It seems that at least using EclipseLink, custom types for which an <code>AttributeConverter</code> exists, are not fully supported:<br><br>
  Within JPQL queries, <strong>neither the actual field type nor the converted database type can be used as a parameter</strong>.
  When using the converted database type, the exception described above occurs.
  When using the actual field type (e.g. <code>LocalDate</code>), it's directly passed to the jdbc driver which doesn't know this type:</p>
  
  <pre><code>Caused by: java.sql.SQLException: Invalid column type
      at oracle.jdbc.driver.OraclePreparedStatement.setObjectCritical(OraclePreparedStatement.java:10495) 
      at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:9974) 
      at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:10799) 
      at oracle.jdbc.driver.OraclePreparedStatement.setObject(OraclePreparedStatement.java:10776) 
      at oracle.jdbc.driver.OraclePreparedStatementWrapper.setObject(OraclePreparedStatementWrapper.java:241)
      at org.eclipse.persistence.internal.databaseaccess.DatabasePlatform.setParameterValueInDatabaseCall(DatabasePlatform.java:2506)
  </code></pre>
  
  <p>I would expect that EclipseLink converts the field type to the java.sql type using the AttributeConverter.
  (see also this bug report: <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=494999" rel="nofollow noreferrer">https://bugs.eclipse.org/bugs/show_bug.cgi?id=494999</a> )</p>
  
  <p>Which leads us to the most important question #4:</p>
  
  <ol start="4">
  <li>Is there a workaround/solution to support java 8 date fields using <code>EclipseLink</code>, <strong>including the possibility to use a query parameters on such a field</strong>?</li>
  </ol>
  
  <p><strong>ADDITIONAL INFO</strong></p>
  
  <ul>
  <li><a href="http://pastebin.com/yMfM2jXF" rel="nofollow noreferrer">AttributeConverter used (for LocalDateTime conversion)</a></li>
  <li><a href="http://pastebin.com/9p9AT8Rc" rel="nofollow noreferrer">Additional information to reproduce exception</a></li>
  </ul>