Nagendra Busam
Nagendra Busam

Reputation: 255

Local date time in entity ignoring last 3 digits of given format on save

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.

Upvotes: 1

Views: 1596

Answers (1)

fiveobjects
fiveobjects

Reputation: 4289

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.

Upvotes: 1

Related Questions