对于数据库表的自增ID , createDate和updateDate 等字段,用JPA注解代替触发器实现,效率会高很多。 
由于这些属性很多entity都有 可以写成两个基本entity :BaseEntity和AbstractEntity 然后其他entity继承BaseEntity即可

BaseEntity

@MappedSuperclass
public class BaseEntity extends AbstractEntity {
@Id
@Column(
name = "ID"
)
@GeneratedValue(
strategy = GenerationType.AUTO
)
private Long id; public BaseEntity() {
} public Long getId() {
return this.id;
} public void setId(Long id) {
this.id = id;
} public boolean equals(Object o) {
if(o == this) {
return true;
} else if(!(o instanceof BaseEntity)) {
return false;
} else {
BaseEntity other = (BaseEntity)o;
if(!other.canEqual(this)) {
return false;
} else {
Long this$id = this.getId();
Long other$id = other.getId();
if(this$id == null) {
if(other$id != null) {
return false;
}
} else if(!this$id.equals(other$id)) {
return false;
} return true;
}
}
} protected boolean canEqual(Object other) {
return other instanceof BaseEntity;
} public int hashCode() {
boolean PRIME = true;
byte result = 1;
Long $id = this.getId();
int result1 = result * 59 + ($id == null?0:$id.hashCode());
return result1;
} public String toString() {
return "BaseEntity(id=" + this.getId() + ")";
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

AbstractEntity

@MappedSuperclass
public abstract class AbstractEntity implements Serializable {
@Column(
name = "CREATE_DATE",
nullable = false,
updatable = false
)
private Date createDate;
@Column(
name = "UPDATE_DATE",
nullable = false
)
private Date updateDate; protected void touchCreateTime() {
this.createDate = new Date();
} protected void touchUpdateTime() {
this.updateDate = new Date();
} @PrePersist
public void fireCreated() {
this.touchCreateTime();
this.touchUpdateTime();
} @PreUpdate
public void fireUpdated() {
this.touchUpdateTime();
} public AbstractEntity() {
} public Date getCreateDate() {
return this.createDate;
} public Date getUpdateDate() {
return this.updateDate;
} public void setCreateDate(Date createDate) {
this.createDate = createDate;
} public void setUpdateDate(Date updateDate) {
this.updateDate = updateDate;
} public boolean equals(Object o) {
if(o == this) {
return true;
} else if(!(o instanceof AbstractEntity)) {
return false;
} else {
AbstractEntity other = (AbstractEntity)o;
if(!other.canEqual(this)) {
return false;
} else {
Date this$createDate = this.getCreateDate();
Date other$createDate = other.getCreateDate();
if(this$createDate == null) {
if(other$createDate != null) {
return false;
}
} else if(!this$createDate.equals(other$createDate)) {
return false;
} Date this$updateDate = this.getUpdateDate();
Date other$updateDate = other.getUpdateDate();
if(this$updateDate == null) {
if(other$updateDate != null) {
return false;
}
} else if(!this$updateDate.equals(other$updateDate)) {
return false;
} return true;
}
}
} protected boolean canEqual(Object other) {
return other instanceof AbstractEntity;
} public int hashCode() {
boolean PRIME = true;
byte result = 1;
Date $createDate = this.getCreateDate();
int result1 = result * 59 + ($createDate == null?0:$createDate.hashCode());
Date $updateDate = this.getUpdateDate();
result1 = result1 * 59 + ($updateDate == null?0:$updateDate.hashCode());
return result1;
} public String toString() {
return "AbstractEntity(createDate=" + this.getCreateDate() + ", updateDate=" + this.getUpdateDate() + ")";
}
}

转自CSDN https://blog.csdn.net/hikeboy/article/details/56006987

数据库表的自增ID createDate和updateDate 用JPA注解代替触发器实现的更多相关文章

  1. 【开发笔记】- 将MySQL数据库表中自增ID从0开始

    命令: 用于清空某表的数据 且让自增的id重新从0开始 truncate table 你的表名

  2. Mysql数据库表的自增主键ID号乱了,需要重新排列。

    Mysql数据库表的自增主键ID号乱了,需要重新排列. 原理:删除原有的自增ID,重新建立新的自增ID. 1,删除原有主键:ALTER TABLE `table_name` DROP `id`; 2, ...

  3. mysql数据库表的自增主键号不规律,重新排列

    mysql数据库表的自增主键ID乱了,需要重新排序. 原理:删除原有的自增ID,重新建立新的自增ID. 1.删除原有主键: ALTER TABLE `table_name` DROP `id`; 2. ...

  4. oracle如何创建表的自增ID(通过触发器)

    Oracle中创建表的自增ID(通过触发器),序列的自增ID和触发器的自增ID的区别 1.新增数据(序列) --创建示例表 -- create table Student( stuId ) not n ...

  5. 如何在MySQl数据库中给已有的数据表添加自增ID?

    由于使用MySQL数据库还没有多久的缘故,在搭建后台往数据库导入数据的时候发现新增的表单是没有自增id的,因次就有了上面这个问题. 解决方法 1.给某一张表先增加一个字段,这里我们就以node_tab ...

  6. SQL SERVER 从其它数据库中复制带自增ID主键的表数据

    SQL SERVER两个结构相同(或不同)的表,互相导入数据,方法有两种: 1.使用SQL SERVER 自带的导出.导入功能,在库名上右击,“任务”,导出数据.导入数据,这个操作具体不就不多讲了. ...

  7. SQL Server数据库表重置自增主键号(通常是指ID)

    执行 DBCC CHECKIDENT ('table_name', NORESEED) 以确定列中的当前最大值 然后使用 DBCC CHECKIDENT ('table_name', RESEED,n ...

  8. oracle中如何创建表的自增ID(通过序列)

    1.什么是序列呢? 序列是一数据库对象,利用它可生成唯一的整数.一般使用序列自动地生成主码值.一个序列的值是由特别的Oracle程序自动生成,因而序列避免了在运用层实现序列而引起的性能瓶颈. Orac ...

  9. sqlserver为数据库表增加自增字段

     需求: 数据库为SQLServer.对已有的数据库表customer加一个序号字段,一次性对所有现存客户加上编号,并在新建客户时自动增加一个编号,数值自增1. 解决方法: 1. 复制表结构.把原 ...

随机推荐

  1. MYSQL导入/迁移后事件不执行

    mysql迁移后事件不执行 查看数据库是否开启事件支持 mysql> show variables like 'event_scheduler'; +-----------------+---- ...

  2. pyqt安装

    一.安装PyQt5 pip install PyQt5 二.安装PyQt-tools pip install PyQt-tools *注:mac不需要安装PyQt-tools,能够正常使用,只支持Wi ...

  3. Vue的基本使用和模版语法

    Vue的基本使用和模版语法 一.Vue概述 Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架 vue 的核心库只关注视图层,不仅易于上手,还便于与第三方库或既有项目 ...

  4. Jmeter- 笔记12 - 性能测试分析 & 性能测试流程

    性能测试分析 场景设计.监视图表: 设计场景:阶梯式.波浪式 监视器: 收集用于性能分析的数据:TPS图表.聚合报告\汇总报告.察看结果树.响应时间.吞吐量 服务器资源监控:cpu.内存.磁盘io 分 ...

  5. jupyter notebook快捷键使用的注意点

    来源:https://zhidao.baidu.com/question/1800695798976401387.html 本文做进一步的阐释: 1.使行出现,但是光标要点击到有line空白区域 直接 ...

  6. .NET平台系列17 .NET5中的ARM64性能

    系列目录     [已更新最新开发文章,点击查看详细] .NET团队使.NET 5大大提高了常规性能和ARM64性能.在<.NET5中的性能改进>博客中可以查看总体改进情况.在这篇文章中, ...

  7. 功率半导体碳化硅(SiC)技术

    功率半导体碳化硅(SiC)技术 Silicon Carbide Adoption Enters Next Phase 碳化硅(SiC)技术的需求继续增长,这种技术可以最大限度地提高当今电力系统的效率, ...

  8. Single Shot Multibox Detection (SSD)实战(上)

    Single Shot Multibox Detection (SSD)实战(上) 介绍了边界框.锚框.多尺度对象检测和数据集.现在,我们将利用这些背景知识构建一个目标检测模型:单次多盒检测(SSD) ...

  9. VB 老旧版本维护系列---有点懵逼的webserver访问

    有点懵逼的webserver访问 '定义webserver地址 Dim postUrl As String = "" '定义webserver所需xml字符串参数 Dim xmlR ...

  10. 基于SSL(TLS)的HTTPS网页下载——如何编写健壮的可靠的网页下载

    源码下载地址案例开发环境:VS2010本案例未使用openssl库,内部提供了sslite.dll库进行TLS会话,该库提供了ISSLSession接口用于建立SSL会话. HTTP协议很简单,写个简 ...