title: JPA Storing OffsetDateTime with ZoneOffset
body: <p>I have the following entity class:</p>
  
  <pre><code>@Entity
  public class Event {
      private OffsetDateTime startDateTime;
      // ...
  }
  </code></pre>
  
  <p>However, <strong>persisting and then reading the entity to/from the database with JPA 2.2 results in a loss of information</strong>: the <code>ZoneOffset</code> of <code>startDateTime</code> changes to <code>UTC</code> (the <code>ZoneOffset</code> used by the database timestamp). For instance:</p>
  
  <pre><code>Event e = new Event();
  e.setStartDateTime(OffsetDateTime.parse("2018-01-02T09:00-05:00"));
  
  e.getStartDateTime().getHour(); // 9
  e.getStartDateTime().getOffset(); // ZoneOffset.of("-05:00")
  // ...
  entityManager.persist(e); // Stored startDateTime as timestamp 2018-01-02T14:00Z
  
  Event other = entityManager.find(Event.class, e.getId());
  other.getStartDateTime().getHour(); // 14 - DIFFERENT
  other.getStartDateTime().getOffset(); // ZoneOffset.of("+00:00") - DIFFERENT
  </code></pre>
  
  <p>I need to use <code>OffsetDateTime</code>: I cannot use <code>ZonedDateTime</code>, because zone rules change (and that also suffers from this information loss anyway). I cannot use <code>LocalDateTime</code>, since an <code>Event</code> happens anywhere in the world and I need the original <code>ZoneOffset</code> from when it happened for accuracy reasons. I can not use <code>Instant</code> because a user fills in the starting time of an event (the event is like an appointment).</p>
  
  <p>Requirements:</p>
  
  <ul>
  <li><p>Need to be able to perform <code>&gt;, &lt;, &gt;=, &lt;=, ==, !=</code> comparisons on timestamps in JPA-QL</p></li>
  <li><p>Need to be able to retrieve the same <code>ZoneOffset</code> as of the <code>OffsetDateTime</code> that was persisted</p></li>
  </ul>