-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for java.time.Instant #163
Comments
@ghost Commented So if you want to persist a point of time with JPA 2.2, the following options appear to me:
Supporting |
|
For others wishing Instants were supported directly, here is my code to support it using an AttributeConverter, as recommended as a stopgap by @lukasj above. import javax.persistence.AttributeConverter; @converter(autoApply = true)
}` |
@psyklopz Unfortunately this approach does not work correctly with all databases. On Microsoft SQL Server for example you will get local time instead of UTC. |
@sergeykad Great point. By convention all our servers are set to UTC. When running on a local dev machine, we pass the time zone (UTC) as part of our connection string to the MySQL JDBC driver. Maybe I worry more than I should, but I don't trust that time zone conversions done in Java are always consistent with those done in the database. I also get uncomfortable that converting from UTC to local and back to UTC is not foolproof. In the hour before (or after) daylight savings we have a wrinkle in time. I wish databases had no concept of time zones. That belongs in the presentation layer and not in the business layers. But I digress... Use my sample code if and only if your database and your Java code are both running in UTC. |
I am in favor of adding support for Instant to JPA specs. |
@lukasj I agree that we should totally do this. |
I've proposed support for Note that it's clear why |
Store |
Original issue #63 mentioned support for java.time.Instant but it's missing in the final 2.2 spec. I haven't found any reason for omitting it.
I feel it's important for supporting pure UTC data flow as described in the #156. Instant already stores epoch internally so it cleanly maps to a UTC timestamp and it would simplify such use case rather than converting back and forth between different (application, database, user) time zones.
I believe that Instant as a machine-timeline view should be the recommended approach for persisting time data over LocalDateTime, OffsetDateTime, ZonedDateTime which are human-timeline views.
Hibernate supports Instant just fine already and allows changing the database timezone via hibernate.jdbc.time_zone.
The text was updated successfully, but these errors were encountered: