对于数据库表的自增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. IDEA Git 操作常见错误处理

    使用 IDEA 的 git 进行操作时报错 更新报错 Git Pull Failed: refusing to merge unrelated histories 提交报错 Push rejected ...

  2. Element-ui Popconfirm气泡确认框的确认及取消事件不生效

    Element-ui 官方文档对 Popconfirm气泡确认框的一些属性及事件的描述不够详细,导致第一次使用时会遇到各种各样的问题 对确定事件及取消事件描述如下: 但是如果给组件绑定@confirm ...

  3. 基于 element-plus 封装一个依赖 json 动态渲染的查询控件

    前情回顾 基于 el-form 封装一个依赖 json 动态渲染的表单控件 Vue3 封装第三方组件(一)做一个合格的传声筒 功能 使用 vue3 + element-plus 封装了一个查询控件,专 ...

  4. .Net Core gRPC 实战(二)

    概述 gRPC 客户端必须使用与服务相同的连接级别安全性.  如调用服务时通道和服务的连接级别安全性不一致,gRPC 客户端就会抛出错误. gRPC 配置使用HTTP gRPC 客户端传输层安全性 ( ...

  5. ZooKeeper学习笔记二:API基本使用

    Grey ZooKeeper学习笔记二:API基本使用 准备工作 搭建一个zk集群,参考ZooKeeper学习笔记一:集群搭建. 确保项目可以访问集群的每个节点 新建一个基于jdk1.8的maven项 ...

  6. 稀疏自编码器及TensorFlow实现

    自动编码机更像是一个识别网络,只是简单重构了输入.而重点应是在像素级重构图像,施加的唯一约束是隐藏层单元的数量. 有趣的是,像素级重构并不能保证网络将从数据集中学习抽象特征,但是可以通过添加更多的约束 ...

  7. ASML光刻机PK 原子弹,难度?

    ASML光刻机PK 原子弹,难度? 一.  物理世界和网络世界的交汇点:光刻机 光刻机的技术有多高级,看看这个知乎提问,可以感受一下: 有人这样形容光刻机:这是一种集合了数学.光学.流体力学.高分子物 ...

  8. 绘制log()函数图像,并在图上标注选定的两个点

    绘制log()函数图像,并在图上标注选定的两个点 import math import matplotlib.pyplot as plt if __name__ == '__main__': x = ...

  9. JUC 并发编程--04 常用的辅助类CountDownLatch , CyclicBarrier , Semaphore , 读写锁 , 阻塞队列,CompletableFuture(异步回调)

    CountDownLatch 相当于一个减法计数器, 构造方法指定一个数字,比如6, 一个线程执行一次,这个数字减1, 当变为0 的时候, await()方法,才开始往下执行,, 看这个例子 Cycl ...

  10. 【NX二次开发】根据根据坐标系、对象旋转视图旋转视图uc6434

    uc6434 (); //旋转视图 参数1:如果输入""则旋转当前工作视图参数2:1.按照ABS旋转视图.2.按照WCS选择视图.3.按照参数3旋转视图.4.按照参数4旋转视图参数 ...