Search code examples
mysqlhibernatedatejava-8localtime

How to store LocalTime in hibernate


I have an entity that has a variable of type LocalTime and I would like to store it in database. So I have two questions:

  1. What data type will the field in mysql be?
  2. What annotation to use for entity?

I do not care for date at all whatsoever.


Solution

  • hibernate-java8 provide a LocalTimeType for persist a LocalTime field.Since hibernate-java8-5.2.+ has been merged into the hibernate-core module.

    Usage

    saving LocalTime as sql time column.

    @Column
    private LocalTime time;
    

    saving LocalTime as sql varchar column.

    @Column(columnDefinition = "varchar(8)")
    private LocalTime time;