Skip to main content
deleted 6 characters in body
Source Link
Mark Rotteveel
  • 110.3k
  • 240
  • 160
  • 233
`TimestampTimestamp.from( zonedDateTime.toInstant() )` 
`tsts.toInstant().atZone( ZoneId.systemDefault() )` 

Note that the ZoneId is not stored in the database, so basically, you are probably better off using a LocalDateTime if this Timestamp conversion is not suitable for you.

Note that the ZoneId is not stored in the database, so basically, you are probably better off using a LocalDateTime if this Timestamp conversion is not suitable for you.

`Timestamp.from( zonedDateTime.toInstant() )` 
`ts.toInstant().atZone( ZoneId.systemDefault() )` 

Note that the ZoneId is not stored in the database, so basically, you are probably better off using a LocalDateTime if this Timestamp conversion is not suitable for you.

Timestamp.from( zonedDateTime.toInstant() ) 
ts.toInstant().atZone( ZoneId.systemDefault() ) 

Note that the ZoneId is not stored in the database, so basically, you are probably better off using a LocalDateTime if this Timestamp conversion is not suitable for you.

added 192 characters in body
Source Link
Vlad Mihalcea
  • 155.9k
  • 85
  • 599
  • 984

However, Hibernate supports also ZonedDateTime, like this.

When saving the ZonedDateTime, the following Timestamp is going to be sent to the PreparedStatement:

`Timestamp.from( zonedDateTime.toInstant() )` 

And, when reading it from the database, the ResultSet will contain a Timestamp that will be transformed to a ZonedDateTime, like this:

`ts.toInstant().atZone( ZoneId.systemDefault() )` 

Note that the ZoneId is not stored in the database, so basically, you are probably better off using a LocalDateTime if this Timestamp conversion is not suitable for you.

However, Hibernate supports also ZonedDateTime.

However, Hibernate supports also ZonedDateTime, like this.

When saving the ZonedDateTime, the following Timestamp is going to be sent to the PreparedStatement:

`Timestamp.from( zonedDateTime.toInstant() )` 

And, when reading it from the database, the ResultSet will contain a Timestamp that will be transformed to a ZonedDateTime, like this:

`ts.toInstant().atZone( ZoneId.systemDefault() )` 

Note that the ZoneId is not stored in the database, so basically, you are probably better off using a LocalDateTime if this Timestamp conversion is not suitable for you.

added 192 characters in body
Source Link
Vlad Mihalcea
  • 155.9k
  • 85
  • 599
  • 984

JPA 2.2 offers support for mapping Java 8 Date/Time API, likebut only for the following types:

However, Hibernate supports also ZonedDateTime, LocalTime, ZonedDateTimeTime, OffsetDateTime or OffsetTime.

So, let's assume we have the following entity:

@Entity(name = "UserAccount") @Table(name = "user_account") public 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 ZonedDateTime subscribedOn; //Getters and setters omitted for brevity } 

Notice that the subscribedOn attribute is a ZonedDateTime Java object.

When persisting the UserAccount:

UserAccount user = new UserAccount() .setId(1L) .setFirstName("Vlad") .setLastName("Mihalcea") .setSubscribedOn( LocalDateTime.of( 2020, 5, 1, 12, 30, 0 ).atZone(ZoneId.systemDefault()) ); entityManager.persist(user); 

Hibernate generates the proper SQL INSERT statement:

INSERT INTO user_account ( first_name, last_name, subscribed_on, id ) VALUES ( 'Vlad', 'Mihalcea', '2020-05-01 12:30:00.0', 1 ) 

When fetching the UserAccount entity, we can see that the ZonedDateTime is properly fetched from the database:

UserAccount userAccount = entityManager.find( UserAccount.class, 1L ); assertEquals( LocalDateTime.of( 2020, 5, 1, 12, 30, 0 ).atZone(ZoneId.systemDefault()), userAccount.getSubscribedOn() ); 

JPA 2.2 offers support for mapping Java 8 Date/Time API, like ZonedDateTime, LocalTime, ZonedDateTimeTime, OffsetDateTime or OffsetTime.

So, let's assume we have the following entity:

@Entity(name = "UserAccount") @Table(name = "user_account") public 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 ZonedDateTime subscribedOn; //Getters and setters omitted for brevity } 

Notice that the subscribedOn attribute is a ZonedDateTime Java object.

When persisting the UserAccount:

UserAccount user = new UserAccount() .setId(1L) .setFirstName("Vlad") .setLastName("Mihalcea") .setSubscribedOn( LocalDateTime.of( 2020, 5, 1, 12, 30, 0 ).atZone(ZoneId.systemDefault()) ); entityManager.persist(user); 

Hibernate generates the proper SQL INSERT statement:

INSERT INTO user_account ( first_name, last_name, subscribed_on, id ) VALUES ( 'Vlad', 'Mihalcea', '2020-05-01 12:30:00.0', 1 ) 

When fetching the UserAccount entity, we can see that the ZonedDateTime is properly fetched from the database:

UserAccount userAccount = entityManager.find( UserAccount.class, 1L ); assertEquals( LocalDateTime.of( 2020, 5, 1, 12, 30, 0 ).atZone(ZoneId.systemDefault()), userAccount.getSubscribedOn() ); 

JPA 2.2 offers support for mapping Java 8 Date/Time API, but only for the following types:

However, Hibernate supports also ZonedDateTime.

So, let's assume we have the following entity:

@Entity(name = "UserAccount") @Table(name = "user_account") public 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 ZonedDateTime subscribedOn; //Getters and setters omitted for brevity } 

Notice that the subscribedOn attribute is a ZonedDateTime Java object.

When persisting the UserAccount:

UserAccount user = new UserAccount() .setId(1L) .setFirstName("Vlad") .setLastName("Mihalcea") .setSubscribedOn( LocalDateTime.of( 2020, 5, 1, 12, 30, 0 ).atZone(ZoneId.systemDefault()) ); entityManager.persist(user); 

Hibernate generates the proper SQL INSERT statement:

INSERT INTO user_account ( first_name, last_name, subscribed_on, id ) VALUES ( 'Vlad', 'Mihalcea', '2020-05-01 12:30:00.0', 1 ) 

When fetching the UserAccount entity, we can see that the ZonedDateTime is properly fetched from the database:

UserAccount userAccount = entityManager.find( UserAccount.class, 1L ); assertEquals( LocalDateTime.of( 2020, 5, 1, 12, 30, 0 ).atZone(ZoneId.systemDefault()), userAccount.getSubscribedOn() ); 
deleted 158 characters in body
Source Link
Vlad Mihalcea
  • 155.9k
  • 85
  • 599
  • 984
Loading
deleted 17 characters in body
Source Link
Vlad Mihalcea
  • 155.9k
  • 85
  • 599
  • 984
Loading
added 41 characters in body
Source Link
Basil Bourque
  • 346.8k
  • 130
  • 950
  • 1.3k
Loading
Source Link
Vlad Mihalcea
  • 155.9k
  • 85
  • 599
  • 984
Loading