Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 342
Quillion Avatar asked Apr 15 '17 00:04

Quillion


People also ask

Does hibernate support LocalDate?

Hibernate gets all required information from the type of the attribute. You can see an example of an entity with attributes of type LocalDate, LocalDateTime, and Duration in the following code snippet. You can then use these attributes in the same way as any other attributes in your Java code.

Which annotation is used to force Hibernate to store years months and days?

You can define the preferred mapping with the @Temporal annotation. As you can see in the following code snippet, it takes a TemporalType enum as a value. The enum allows you to select the SQL type (DATE, TIME or TIMESTAMP) which you want to use.

What is timestamp in hibernate?

When you persist a new MyEntity, Hibernate will get the current time from the VM and store it as the creation and update timestamp. As you can see in the log output, Hibernate gets a new timestamp for each attribute. The creation and update timestamp will therefore not be the same even if the entity was never updated.

What is @basic annotation in hibernate?

We can use the @Basic annotation to mark a basic type property: @Entity public class Course { @Basic @Id private int id; @Basic private String name; ... } In other words, the @Basic annotation on a field or a property signifies that it's a basic type and Hibernate should use the standard mapping for its persistence.


1 Answers

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;
like image 79
holi-java Avatar answered Sep 29 '22 13:09

holi-java