What is the use of the @Temporal annotation in Hibernate?

前端 未结 7 969
北海茫月
北海茫月 2020-11-30 18:06

The Hibernate Documentation has the information below for the @Temporal annotation:

In plain Java APIs, the temporal precision of time is

7条回答
  •  悲&欢浪女
    2020-11-30 18:38

    @Temporal is a JPA annotation which can be used to store in the database table on of the following column items:

    1. DATE (java.sql.Date)
    2. TIME (java.sql.Time)
    3. TIMESTAMP (java.sql.Timestamp)

    Generally when we declare a Date field in the class and try to store it.
    It will store as TIMESTAMP in the database.

    @Temporal
    private Date joinedDate;
    

    Above code will store value looks like 08-07-17 04:33:35.870000000 PM

    If we want to store only the DATE in the database,
    We can use/define TemporalType.

    @Temporal(TemporalType.DATE)
    private Date joinedDate;
    

    This time, it would store 08-07-17 in database

    There are some other attributes as well as @Temporal which can be used based on the requirement.

提交回复
热议问题