I have weird problem when storing DateTime in database using hibernate and joda time.
I have a bean like this :
import org.joda.time.DateTime;
public class RelationshipEntity {
@Column(name = "date_revision")
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime dateRevision;
// other fields
}
I have set the timezone to Europe/Paris (GMT+1) in the application with TimeZone.setDefault(TimeZone.getTimeZone("Europe/Paris"));
If I save the entity with a revision date to 2017-01-19 13:24
into the database, I can see the date in the database has been set to 2017-01-19 12:24
and I don't understand why.
The database timezone also seems to be Europe/Paris because :
SELECT SYSDATETIME(); //2017-01-19 14:41
SELECT SYSUTCDATETIME(); // 2017-01-19 13:41
As I use spring-boot I tried to add theses properties in application.yml
jadira.usertype.autoRegisterUserTypes: true
jadira.usertype.databaseZone: jvm
jadira.usertype.javaZone: jvm
and start the application with the argument -Duser.timezone=Europe/Paris
but it still saves the date in UTC into the database.
To solve the problem I changed DateTime
to LocalDateTime
:
@Column(name = "date_revision")
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
private DateLocalTime dateRevision;
With this change, the dateRevision in the database is the same I set in my java bean.
Can someone explain me why it converts the date to UTC when I use the type org.joda.time.DateTime
?
NOTE : I use Hibernate 4 and joda-time 2.5