Search code examples
javapostgresqlspring-boothibernatelocaldatetime

How to insert Date and Time from script into PostgreSQL database while using java.time.LocalDateTime


I'm having a problem with inserting Time correctly into the PostgreSQL database. Making web app with SpringBoot, using JPARepository and hibernate. Using this dialect:

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQL95Dialect

I have a class:

@Entity
public class Class {
//other stuff
@Column(name = "date_and_time")
private LocalDateTime dateAndTime;

I need some test data and when I start my app with this script:

insert into class(date_and_time) values (DATE('2020-06-10 18:43:20');

the result in my database looks like this 2020-06-10 00:00:00

Is there any way I can insert time properly in my database?


Solution

  • You are casting your datetime to only date when you use the ‘DATE’ function, so you lose the time information. Simply remove the function and it should work.