There is no direct way to do this (see Setting default values for columns in JPA).
What you could do is to perform a select SYSDATE from dual
and use the result to set your property.
The method to get the sysdate could be in your Spring Data Repository
@Query(value=`select SYSDATE from dual`, nativeQuery=true)
Date currentDate();
You could set the value in a @PrePersist
Listener (see onSave() (for any Entity saved with Hibernate/Spring Data Repositories) ).
But I think you can't perform queries in those listeners, so the next thing would be to create a custom implementation of Spring Data's save
method, so that it gets such a value and keeps it available for the Listener, before actually saving anything. Alternatively one could use a separate connection of the query.
Obviously, this all adds another database roundtrip, which is rather expensive.
An alternative would be to get the current time of the server once and use that just to determine the correct offset to use and create the timestamps locally, using that offset. This is much faster and easier but breaks when application server and database server have different daylight saving time rules.