Hibernate @Temporal annotations Handle the mappings between Java and MySQL time and date types

Source: Internet
Author: User
Tags time and date

2017-4-2

Use java.util.Date and Java.util.Calendar in Java to represent the time, date type. The use of year , date, time, TIMESTAMP, DATETIME Five data types in Mysql represents the date, which leads to the question of how the Java date type maps to the date type of the database. This article summarizes the solutions for using Hibernate @Temporal annotations.

to decorate the data type of a Java entity class using @Temporal annotations

    • Specifies @Temporal the Value property, which corresponds to the database mapping relationship:
      • temporaltype.date  -->  date
      • temporaltype.time  -->  time
      • temporaltype.timestamp  -->  timestamp
    • For example code:
1 @Temporal (temporaltype.date) 2  Public Date Birth; 3 // get, set method

Differences between Mysql time and date types

differences between MySQL five time and date types
type

occupied

space

format scope
Year 1byte ' YYYY '

Year (4): ' 1901 ' to ' 2155 '

Year (2): ' 0 ' to ' 69 ' for 2000-2,069 years

DATE 3byte ' Yyyy-mm-dd ' ' 1000-01-01 ' to ' 9999-12-31 '
time 3byte " HH:MM:SS ' -838:59:59 "  to  ' 838:59:59
timestamp 4byte " Yyyy-mm-dd hh:mm:ss '

' 1970-01-01 00:00:01 '  UTC  to 

" 2038-01-19 03:14:07 '  UTC

Datetime 8byte ' Yyyy-mm-dd HH:MM:SS '

' 1000-01-01 00:00:00 ' to

' 9999-12-31 23:59:59 '

Reference Blog: http://lgdvsehome.blog.51cto.com/3360656/1243676

Hibernate @Temporal annotations Handle the mappings between Java and MySQL time and date types

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.