title: How to persist LocalDate with JPA?
body: <p>I want to store Date without time into my database. So, I choose to use <code>LocalDate</code> type. </p>
  
  <p>As mentioned in <a href="https://thoughts-on-java.org/persist-localdate-localdatetime-jpa/" rel="noreferrer">this article</a>, I use a JPA converter to convert <code>LocalDate</code> to <code>Date</code>.</p>
  
  <p>However, I have some troubles when I want to persist my entity (with POST and PUT requests).</p>
  
  <p><strong>Error</strong></p>
  
  <pre><code>2019-02-23 11:26:30.254  WARN 2720 --- [-auto-1-exec-10] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Expected array or string.; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Expected array or string.
   at [Source: (PushbackInputStream); line: 1, column: 104] (through reference chain: ...entity.MyObject["startdate"])]
  
  org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.springframework.http.ResponseEntity]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.springframework.http.ResponseEntity` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
   at [Source: (PushbackInputStream); line: 1, column: 2]
  </code></pre>
  
  <p><strong>Code</strong></p>
  
  <p><em>Converter</em></p>
  
  <pre><code>package ...entity;
  
  import javax.persistence.AttributeConverter;
  import javax.persistence.Converter;
  import java.time.LocalDate;
  import java.sql.Date;
  
  @Converter(autoApply = true)
  public class LocalDateAttributeConverter implements AttributeConverter&lt;LocalDate, Date&gt; {
  
      @Override
      public Date convertToDatabaseColumn(LocalDate locDate) {
          return (locDate == null ? null : Date.valueOf(locDate));
      }
  
      @Override
      public LocalDate convertToEntityAttribute(Date sqlDate) {
          return (sqlDate == null ? null : sqlDate.toLocalDate());
      }
  }
  </code></pre>
  
  <p><em>Entity</em></p>
  
  <pre><code>package ...entity;
  
  import org.hibernate.annotations.ColumnDefault;
  
  import javax.persistence.*;
  import java.time.LocalDate;
  import java.util.HashSet;
  import java.util.Set;
  
  @Entity
  public class MyObject {
  
      @Id
      private String id;
      private LocalDate startdate;
      private LocalDate enddate;
  
      public MyObject() {}
  
      public MyObject(LocalDate enddate) {
          this.startdate = LocalDate.now();
          this.enddate = enddate;
      }
  
      ...
  }
  </code></pre>
  
  <p><em>"Main"</em></p>
  
  <pre><code>private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  MyObject myobject = new MyObject(LocalDate.parse("2019-03-01", formatter));
  </code></pre>
  
  <p>Thanks for help.</p>
  
  <p><strong>EDIT 1 : Print of MyObject</strong></p>
  
  <pre><code> HttpHeaders headers = new HttpHeaders();
   headers.setContentType(MediaType.APPLICATION_JSON);
   HttpEntity&lt;String&gt; entity = new HttpEntity&lt;&gt;(this.toJsonString(myObject), headers);
   System.out.println(entity.toString());
  
   // &lt;{"id":"ba6649e4-6e65-4f54-8f1a-f8fc7143b05a","startdate":{"year":2019,"month":"FEBRUARY","dayOfMonth":23,"dayOfWeek":"SATURDAY","era":"CE","dayOfYear":54,"leapYear":false,"monthValue":2,"chronology":{"id":"ISO","calendarType":"iso8601"}},"enddate":{"year":2019,"month":"MARCH","dayOfMonth":1,"dayOfWeek":"FRIDAY","era":"CE","dayOfYear":60,"leapYear":false,"monthValue":3,"chronology":{"id":"ISO","calendarType":"iso8601"}}},[Content-Type:"application/json"]&gt;
  </code></pre>