java扩展Hibernate注解支持java8新时间类型

时间:2021-09-12 07:43:38 

扩展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;
    }
}

标签:java,Hibernate,注解,java8
0
投稿

猜你喜欢

  • 基于socket和javaFX简单文件传输工具

    2022-11-04 12:37:42
  • C#实现把txt文本数据快速读取到excel中

    2022-10-15 02:59:10
  • Android实现自动点击无障碍服务功能的实例代码

    2022-08-31 12:14:26
  • Android控件CardView实现卡片布局

    2022-12-05 02:10:32
  • Android sdutio配置Zxing进行扫码功能的实现方法

    2023-12-12 15:40:13
  • SpringAOP如何获取方法参数上的注解

    2021-09-19 23:22:18
  • c#(Socket)同步套接字代码示例

    2023-12-16 04:03:13
  • java图片缩放实现图片填充整个屏幕

    2021-06-09 18:30:07
  • Unity命令行打包WebGL的示例代码

    2023-02-02 10:05:13
  • springboot 参数格式校验操作

    2023-10-16 08:53:32
  • C# 调用API函数弹出映射网络驱动器对话框问题

    2023-03-25 15:41:05
  • 详解Android开发中Fragment的使用

    2023-05-02 20:32:25
  • 详解在LINUX上部署带有JAR包的JAVA项目

    2022-12-30 21:07:14
  • C#中DataGridView常用操作实例小结

    2023-03-09 23:55:16
  • Java 实战练手项目之酒店管理系统的实现流程

    2022-07-30 09:05:10
  • 详解Java实现单例的五种方式

    2021-06-11 03:54:03
  • java实现消息队列的两种方式(小结)

    2022-07-09 08:12:13
  • Android如何通过scheme跳转界面

    2021-08-11 05:19:41
  • java如何判断一个对象是否为空对象

    2023-12-11 06:53:59
  • Java的Socket网络编程基础知识入门教程

    2021-11-19 10:10:37
  • asp之家 软件编程 m.aspxhome.com