对于数据库表的自增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. 11.19 rpm:RPM包管理器

    rpm命令的全称是Red Hat Package Manager(Red Hat包管理器),几乎所有的Linux发行版本都使用了这种形式的命令管理.安装.更新和卸载软件. 概括地说,rpm命令包含了五 ...

  2. Python数模笔记-PuLP库(1)线性规划入门

    1.什么是线性规划 线性规划(Linear programming),在线性等式或不等式约束条件下求解线性目标函数的极值问题,常用于解决资源分配.生产调度和混合问题.例如: max fx = 2*x1 ...

  3. linux上传启动项目命令

    使用Xshell 或其他远程链接登录工具登录服务器后 1.切换用户到root: sudo -i 账户密码 注意:可直接将jar包放入root用户目录下,避免有可能因为服务器文件夹权限设置导致在指定文件 ...

  4. libevent中的bufferevent原理

    以前的文章看过缓冲区buffer了,libevent用bufferevent来负责管理缓冲区与buffer读写事件.       今天就带大家看下evbuffer.c,使用bufferevent处理事 ...

  5. Python+Selenium - iframe定位

    元素在iframe中.在html当中,内嵌了另一个html (iframe) 分辨元素是否在iframe当中 在代码当中,从当前的html切换到iframe当中的html,然后在元素定位 切换方式:d ...

  6. ZooKeeper学习笔记三:使用ZooKeeper实现一个简单的配置中心

    作者:Grey 原文地址:ZooKeeper学习笔记三:使用ZooKeeper实现一个简单的配置中心 前置知识 完成ZooKeeper集群搭建以及熟悉ZooKeeperAPI基本使用 需求 很多程序往 ...

  7. OneFlow系统设计

    OneFlow系统设计 本文的主要内容如下: OneFlow 的设计目标 OneFlow 的特色一:Actor 机制 OneFlow 的特色二:SBP 机制 总结 一.OneFlow 的设计目标 On ...

  8. 用于ONNX的TensorRT后端

    用于ONNX的TensorRT后端 解析ONNX模型以使用TensorRT执行. 另请参阅TensorRT文档. 有关最近更改的列表,请参见changelog. 支持的TensorRT版本 Maste ...

  9. TVM部署和集成Deploy and Integration

    TVM部署和集成Deploy and Integration 本文包含如何将TVM部署到各种平台以及如何将其与项目集成. 与传统的深度学习框架不同.TVM堆栈分为两个主要组件: TVM编译器,完成所有 ...

  10. 硬件安全模块如何启用AUTOSAR

    硬件安全模块如何启用AUTOSAR How hardware security modules enable AUTOSAR 越来越复杂的软件和车内连接需要越来越多的加密保护.这种保护也必须由经典的实 ...