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:
I do not care for date at all whatsoever.
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.
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.
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.
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.
hibernate-java8 provide a LocalTimeType for persist a LocalTime
field.Since hibernate-java8-5.2.+ has been merged into the hibernate-core module.
saving LocalTime
as sql time column.
@Column
private LocalTime time;
saving LocalTime
as sql varchar column.
@Column(columnDefinition = "varchar(8)")
private LocalTime time;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With