com.lushapp.common.orm.entity.BaseEntity.java Source code

Java tutorial

Introduction

Here is the source code for com.lushapp.common.orm.entity.BaseEntity.java

Source

/**
 *  Copyright (c) 2014 http://www.lushapp.wang
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.lushapp.common.orm.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.lushapp.common.excel.annotation.Excel;
import com.lushapp.common.utils.DateUtil;

import org.apache.commons.lang3.builder.ToStringBuilder;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
 * entity. <br>
 * id?????????. <br>
 * ???getId()?id???. <br>
 * 2014-12-15 wencp:?(??,?)????ID.
 * 
 * @author : honey.zhao@aliyun.com  
 * @date : 2014-12-21 ?11:12:07
 */
// @Column(name="...") name
// @Table 
// @UniqueConstraint ?
// @Version ??timestamp
// @Transient ????
// @Basic fetch?optional??null
// @Enumerated 
// @Temporal ??Timestamp
// @Lob @Basic???
// @Embeddable ?
// @Embedded @Embeddable
// @AttributeOverrides ?
// @AttributeOverride ?@AttributeOverrides
// @SecondaryTables 
// @SecondaryTable @SecondaryTables
// @GeneratedValue ?Auto
// JPA 
// @AttributeOverride(name = "id", column = @Column(name = "base_id"))
@MappedSuperclass
public class BaseEntity extends AutoEntity implements Serializable, Cloneable {

    /**
     * 
     */
    private static final long serialVersionUID = 2142201445199112425L;

    public static final String DATE_FORMAT = "yyyy-MM-dd";

    public static final String TIME_FORMAT = "HH:mm:ss";

    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static final String TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss.S";

    public static final String TIMEZONE = "GMT+08:00";

    public static String date2String(Date date, String dateFormat) {
        return DateUtil.format(date, dateFormat);
    }

    public static <T extends Date> T string2Date(String dateString, String dateFormat, Class<T> targetResultType) {
        return (T) DateUtil.parse(dateString, dateFormat, targetResultType);
    }

    /**
     * ?? (0) (1) (2) ?(3) (4)
     */
    protected Integer status = StatusState.normal.getValue();
    /**
     * ?(??,?)
     */
    protected Integer version;

    /**
     * ??
     */
    @Excel(exportName = "", exportFieldWidth = 30)
    protected String createUser;
    /**
     * 
     */
    @Excel(exportName = "", exportFieldWidth = 30)
    protected Date createTime;

    /**
     *  ??
     */
    protected String updateUser;
    /**
     * 
     */
    protected Date updateTime;

    public BaseEntity() {
        super();
    }

    /**
     * ??
     */
    @Column(name = "STATUS", columnDefinition = "INT default 0")
    public Integer getStatus() {
        return status;
    }

    /**
     * ???
     */
    @Transient
    public String getStatusView() {
        StatusState s = StatusState.getStatusState(status);
        String str = "";
        if (s != null) {
            str = s.getDescription();
        }
        return str;
    }

    /**
     *  ??
     * 
     * @param status
     *            ??
     */
    public void setStatus(Integer status) {
        this.status = status;
    }

    /**
     * ?(??)
     */
    @Version
    @Column(name = "VERSION", columnDefinition = "INT default 1")
    public Integer getVersion() {
        return version;
    }

    /**
     *  ?(??)
     * 
     * @param version
     *            ?(??)
     */
    public void setVersion(Integer version) {
        this.version = version;
    }

    /**
     *  ??
     */
    @Column(name = "CREATE_USER", updatable = false, length = 36)
    public String getCreateUser() {
        return createUser;
    }

    /**
     *   ??
     * 
     * @param createUser
     *            ??
     */
    public void setCreateUser(String createUser) {
        this.createUser = createUser;
    }

    /**
     * .
     */
    // JSON??
    @JsonFormat(pattern = DATE_TIME_FORMAT, timezone = TIMEZONE)
    @Column(name = "CREATE_TIME", updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    public Date getCreateTime() {
        return createTime;
    }

    /**
     *  
     * 
     * @param createTime
     *            
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     *  ??
     */
    @Column(name = "UPDATE_USER", length = 36)
    public String getUpdateUser() {
        return updateUser;
    }

    /**
     *   ??
     * 
     * @param updateUser
     *            ??
     */
    public void setUpdateUser(String updateUser) {
        this.updateUser = updateUser;
    }

    /**
     * 
     */
    // JSON??
    @JsonFormat(pattern = DATE_TIME_FORMAT, timezone = TIMEZONE)
    @Column(name = "UPDATE_TIME")
    public Date getUpdateTime() {
        return updateTime;
    }

    /**
     *  
     * 
     * @param updateTime
     *            
     */
    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public BaseEntity clone() {
        BaseEntity o = null;
        try {
            o = (BaseEntity) super.clone();// Objectclone()??
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return o;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}