If we set the default MySQL time zone to UTC:
{code:java} default_time_zone='+00:00' {code}
And use the following mapping:
{code:java} @Entity(name = "UserAccount") @Table(name = "user_account") public static class UserAccount {
@Id private Long id;
@Column(name = "first_name", length = 50) private String firstName;
@Column(name = "last_name", length = 50) private String lastName;
@Column(name = "subscribed_on") private LocalDate subscribedOn;
public Long getId() { return id; }
public UserAccount setId(Long id) { this.id = id; return this; }
public String getFirstName() { return firstName; }
public UserAccount setFirstName(String firstName) { this.firstName = firstName; return this; }
public String getLastName() { return lastName; }
public UserAccount setLastName(String lastName) { this.lastName = lastName; return this; }
public LocalDate getSubscribedOn() { return subscribedOn; }
public UserAccount setSubscribedOn(LocalDate subscribedOn) { this.subscribedOn = subscribedOn; return this; } } {code}
When persisting the following entity:
{code:java} UserAccount user = new UserAccount() .setId(1L) .setFirstName("Vlad") .setLastName("Mihalcea") .setSubscribedOn( LocalDate.of( 2013, 9, 29 ) );
entityManager.persist(user); {code}
Hibernate generates the following SQL INSERT statement:
insert into user_account (first_name, last_name, subscribed_on, id) values (?, ?, ?, ?)"], Params: \ [(Vlad, Mihalcea, 2013-09-29, 1)
However, when fetching it:
{code:java} assertEquals( LocalDate.of( 2013, 9, 29 ), userAccount.getSubscribedOn() ); {code}
We get the following failure message:
{code:java} Expected :2013-09-29 Actual :2013-09-28 {code}
This is because in the database the following row was inserted:
{code:java} | id | first_name | last_name | subscribed_on | |----|------------|-----------|---------------| | 1 | Vlad | Mihalcea | 28-09-13 | {code}
Setting the timezone property does not fix also emulates the problem even if the server timezone is not set :
{code:java} @Override protected void additionalProperties(Properties properties) { properties.setProperty(AvailableSettings.JDBC_TIME_ZONE, "UTC"); } {code}
because this is I think java.sql.Date and LocalDate should not taken into consideration be affected by `LocalDateType` timezone conversions as they shouldn’t behave like Timestamp types that contain time information .
|
|