Java tutorial
/** * Copyright © 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.pansky.integration.common.persistence; import java.io.Serializable; import java.util.Date; import javax.persistence.Column; import javax.persistence.FetchType; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.MappedSuperclass; import javax.persistence.PrePersist; import javax.persistence.PreUpdate; import javax.persistence.Temporal; import javax.persistence.TemporalType; import javax.persistence.Transient; import org.apache.commons.lang3.StringUtils; import org.hibernate.annotations.NotFound; import org.hibernate.annotations.NotFoundAction; import org.hibernate.search.annotations.Analyze; import org.hibernate.search.annotations.DateBridge; import org.hibernate.search.annotations.Field; import org.hibernate.search.annotations.Index; import org.hibernate.search.annotations.Resolution; import org.hibernate.search.annotations.Store; import org.hibernate.validator.constraints.Length; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonIgnore; import com.pansky.integration.common.utils.DateUtils; import com.pansky.integration.modules.sys.entity.User; import com.pansky.integration.modules.sys.utils.UserUtils; /** * ?Entity * @author ThinkGem * @version 2013-05-28 */ @MappedSuperclass public abstract class DataEntity<T> extends BaseEntity<T> implements Serializable { private static final long serialVersionUID = 1L; protected String remarks; // protected User createBy; // protected Date createDate;// protected User updateBy; // protected Date updateDate;// protected String delFlag; // 012 protected Date createDateStart; protected Date createDateEnd; protected Date updateDateStart; protected Date updateDateEnd; public DataEntity() { super(); this.delFlag = DEL_FLAG_NORMAL; } @PrePersist public void prePersist() { User user = UserUtils.getUser(); if (StringUtils.isNotBlank(user.getId())) { this.updateBy = user; this.createBy = user; } this.updateDate = new Date(); this.createDate = this.updateDate; } @PreUpdate public void preUpdate() { User user = UserUtils.getUser(); if (StringUtils.isNotBlank(user.getId())) { this.updateBy = user; } this.updateDate = new Date(); } @Length(min = 0, max = 255) public String getRemarks() { return remarks; } public void setRemarks(String remarks) { this.remarks = remarks; } @JsonIgnore @ManyToOne(fetch = FetchType.LAZY) @NotFound(action = NotFoundAction.IGNORE) @JoinColumn(name = "create_by") public User getCreateBy() { return createBy; } public void setCreateBy(User createBy) { this.createBy = createBy; } @Temporal(TemporalType.TIMESTAMP) @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") @Column(name = "create_date") public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } @JsonIgnore @ManyToOne(fetch = FetchType.LAZY) @NotFound(action = NotFoundAction.IGNORE) @JoinColumn(name = "update_by") public User getUpdateBy() { return updateBy; } public void setUpdateBy(User updateBy) { this.updateBy = updateBy; } @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") @Field(index = Index.YES, analyze = Analyze.NO, store = Store.YES) @DateBridge(resolution = Resolution.DAY) @Column(name = "update_date") public Date getUpdateDate() { return updateDate; } public void setUpdateDate(Date updateDate) { this.updateDate = updateDate; } @Length(min = 1, max = 1) @Field(index = Index.YES, analyze = Analyze.NO, store = Store.YES) @Column(name = "del_flag", length = 1) public String getDelFlag() { return delFlag; } public void setDelFlag(String delFlag) { this.delFlag = delFlag; } @Temporal(TemporalType.DATE) @Transient public Date getCreateDateStart() { return DateUtils.getDateStart(createDateStart); } public void setCreateDateStart(Date createDateStart) { this.createDateStart = createDateStart; } @Temporal(TemporalType.DATE) @Transient public Date getCreateDateEnd() { return DateUtils.getDateEnd(createDateEnd); } public void setCreateDateEnd(Date createDateEnd) { this.createDateEnd = createDateEnd; } @Temporal(TemporalType.DATE) @Transient public Date getUpdateDateStart() { return DateUtils.getDateStart(updateDateStart); } public void setUpdateDateStart(Date updateDateStart) { this.updateDateStart = updateDateStart; } @Temporal(TemporalType.DATE) @Transient public Date getUpdateDateEnd() { return DateUtils.getDateEnd(updateDateEnd); } public void setUpdateDateEnd(Date updateDateEnd) { this.updateDateEnd = updateDateEnd; } }