Java extended Hibernate annotations support java8 new Time type _java

Source: Internet
Author: User

Extended Hibernate annotation @creationtimestamp, @UpdateTimestamp support Java8 new Time type
Hibernate version:4.3.5.final

Copy Code code as follows:

Package com.hibernate.annotation;

Import org.hibernate.HibernateException;
Import org.hibernate.tuple.AnnotationValueGeneration;
Import Org.hibernate.tuple.ValueGenerator;

Import java.lang.annotation.Annotation;
Import Java.sql.Time;
Import Java.sql.Timestamp;
Import Java.time.LocalDate;
Import Java.time.LocalDateTime;
Import Java.time.LocalTime;
Import Java.util.Calendar;
Import Java.util.Date;

/**
* Created with IntelliJ idea.
* User:zhonggang
* date:14-4-16
* Time: 11:40
* <p/>
* Support JAVA8 New Time Package
*
* @see Java.time.LocalDateTime
* @see Java.time.LocalDate
* @see Java.time.LocalTime
*/
Public abstract class Abstracttimegeneration<a extends annotation> implements Annotationvaluegeneration<a > {
protected valuegenerator<?> Generator;

@Override
public void Initialize (A annotation, class<?> propertytype) {
if (LocalDateTime.class.isAssignableFrom (propertytype)) {
Generator = (Session, owner)-> Localdatetime.now ();
else if (LocalDate.class.isAssignableFrom (propertytype)) {
Generator = (Session, owner)-> Localdate.now ();
else if (LocalTime.class.isAssignableFrom (propertytype)) {
Generator = (Session, owner)-> Localtime.now ();
else if (Java.sql.Date.class.isAssignableFrom (propertytype)) {
Generator = (Session, owner)-> new Java.sql.Date (New Date (). GetTime ());
else if (Time.class.isAssignableFrom (propertytype)) {
Generator = (Session, owner)-> new Time (New Date (). GetTime ());
else if (Timestamp.class.isAssignableFrom (propertytype)) {
Generator = (Session, owner)-> new Timestamp (New Date (). GetTime ());
else if (Date.class.isAssignableFrom (propertytype)) {
Generator = (Session, owner)-> new Date ();
else if (Calendar.class.isAssignableFrom (propertytype)) {
Generator = (Session, owner)-> {
Calendar calendar = Calendar.getinstance ();
Calendar.settime (New Date ());
return calendar;
};
} else {
throw new Hibernateexception ("Unsupported property type for generator annotation" + annotation.getclass (). Getsimplename ());
}
}

@Override
Public valuegenerator<?> Getvaluegenerator () {
return generator;
}

@Override
public Boolean referencecolumninsql () {
return false;
}

@Override
Public String Getdatabasegeneratedreferencedcolumnvalue () {
return null;
}
}

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.