程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java擴大Hibernate注解支撐java8新時光類型

java擴大Hibernate注解支撐java8新時光類型

編輯:關於JAVA

java擴大Hibernate注解支撐java8新時光類型。本站提示廣大學習愛好者:(java擴大Hibernate注解支撐java8新時光類型)文章只能為提供參考,不一定能成為您想要的結果。以下是java擴大Hibernate注解支撐java8新時光類型正文


擴大Hibernate注解@CreationTimestamp,@UpdateTimestamp支撐Java8新的時光類型
Hibernate version: 4.3.5.Final


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/>
 * 支撐Java8新的時光包
 *
 * @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;
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved