When the associated database server and the application server are running on different time zones, retrieving, persisting and updating the current instant of time of the application server is not a good way to go. The database server should always be asked for the current time in place of the application server.
Thus, doing like the following on the middle tier JPA itself would be a wrong way to go.
Entity entity = new Entity();
entity.setTimestamp(new Timestamp(new java.util.Date().getTime()));
// Persist or merge the entity.
Or
Entity entity = new Entity();
entity.setLocalDateTime(java.time.LocalDateTime.now(ZoneOffset.UTC));
// Persist or merge the entity.
Or
Entity entity = new Entity();
entity.setZonedDateTime(java.time.ZonedDateTime.now(ZoneOffset.UTC));
// Persist or merge the entity.
Or
Entity entity = new Entity();
entity.setDateTime(org.joda.time.DateTime.now(org.joda.time.DateTime.DateTimeZone.UTC));
// Persist or merge the entity.
etc along with insertable = false, updatable = false
for the respective field in the entity.
JPA allows the retrieval of date-time / timestamp from the database server.
javax.persistence.criteria.CriteriaBuilder#Expression<Date> currentDate()
javax.persistence.criteria.CriteriaBuilder#Expression<Time> currentTime()
javax.persistence.criteria.CriteriaBuilder#Expression<Timestamp> currentTimestamp()
Naturally, the same is true for JPQL as well.
But there seems no way which JPA can exhibit to persist and merge the current time of the database server itself by delegating the task of persisting and merging the current time to the database server itself.
I am not interested in decorating date-time related fields with @Version
as it has a completely different purpose of acquiring a row level optimistic lock in the middle tier JPA itself (optimistic locking using timestamp itself in turn also suffers from certain special drawbacks).
I do not much consider the jungle of RDBMS while using a persistence provider in the middle tier.
So, except for the functionality being adhered to RDBMS like CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
, does JPA or a particular persistence provider itself has a way to deal with the current time of the database server for two idempotent operations namely "persist" and "merge"?
I am currently concerned with Hibernate and EclipseLink.