1

I have an entity defined as below. Mine is a spring data jpa application.

I am using spring boot version 1.5.4.RELEASE

When I invoke service from controller with createDttm

cretDttm=2019-06-11T11:08:03.520808

it's being saved/updated as 2019-06-11 11:08:03.520000. It's discarding last 3 digits of the local date time. Any pointers to fix the same.

@Setter
@Getter
@NoArgsConstructor
@Entity
@ToString
@Table(name = "FOO")
public class Foo {

  @Id
  @GeneratedValue
  @Column(name = "USER_ID")
  private BigInteger userId;

  @Column(name = "USER_NAME")
  private String userName;

  @Column(name = "CRET_DTTM", updatable = false)
  private LocalDateTime cretDttm;

}
@SpringBootApplication
@EntityScan(basePackageClasses = {Application.class, Jsr310JpaConverters.class})
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }

}

UPDATE : The default Jsr310JpaConverters.class doesn't have converter to support java.sql.Timestamp. I created custom converter as mentioned in https://thoughts-on-java.org/persist-localdate-localdatetime-jpa/#highlighter_743982. Annotated the same as mentioned here JPA Cannot Deserialize Java 8 LocalDateTime. It's working now. Thank you.

Nagendra Busam
  • 235
  • 1
  • 5
  • 19

1 Answers1

1

The below program shows LocalDateTime preserves till nanos (9 digits):

public static void main(String[] args) {
    LocalDateTime dateTime = LocalDateTime.parse("2019-06-11T11:08:03.520808999");
    System.out.println(dateTime.toString());
    System.out.println(dateTime.getNano());
}

Can you check with your database column type, whether it holds till millis precision or more? I am suspecting your database is truncating the micro granularity (6 digits to 3). Choose the right data type if your database supports micros and nanos.

fiveelements
  • 3,649
  • 1
  • 17
  • 16
  • My database is DB2, column size 26. I am able to see till I Invoke repsority.save(myentity).How should is choose one for micros? – Nagendra Busam Jul 29 '19 at 23:04
  • @NagendraBusam what is the data type of the column to store this data-time value? – fiveelements Jul 29 '19 at 23:07
  • db column is TIMESTAMP – Nagendra Busam Jul 29 '19 at 23:10
  • I ran a simple test in my Spring Boot app (LocalDateTime data type) connecting to PostgreSQL (timestamp data type) and it is preserving up to micros when I gave nanos in input. Would you please check with a different database like PostgreSQL or SQLite or Oracle? I am not sure whether something is happening at the DB2 JDBC driver level for LocalDateTime. Would you please also try using `java.sql.Timestamp` (convert `java.time.LocalDateTime` to `java.sql.Timestamp` before saving) as the datatype of the ORM field? – fiveelements Jul 29 '19 at 23:29
  • The default Jsr310JpaConverters.class doesn't have converter to support java.sql.Timestamp. I created custom converter as mentioned in https://thoughts-on-java.org/persist-localdate-localdatetime-jpa/#highlighter_743982. Annotated the same as mentioned here https://stackoverflow.com/questions/42569951/jpa-cannot-deserialize-java-8-localdatetime. It's working now. Thank you. – Nagendra Busam Jul 30 '19 at 13:43
  • @NagendraBusam What is your JPA and Hibernate versions? Looks like the versions I am using (JPA 2.2 and Hibernate 5.3.7) does not need this attribute converter. However, the older versions need it. – fiveelements Jul 30 '19 at 14:24
  • spring-data-jpa-1.11.4.RELEASE.jar hibernate-core-5.0.12.Final.jar. I think default ones coming w/ spring boot version 1.5.4.RELEASE – Nagendra Busam Jul 30 '19 at 14:36