数据库表的自增ID createDate和updateDate 用JPA注解代替触发器实现
对于数据库表的自增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注解代替触发器实现的更多相关文章
- 【开发笔记】- 将MySQL数据库表中自增ID从0开始
命令: 用于清空某表的数据 且让自增的id重新从0开始 truncate table 你的表名
- Mysql数据库表的自增主键ID号乱了,需要重新排列。
Mysql数据库表的自增主键ID号乱了,需要重新排列. 原理:删除原有的自增ID,重新建立新的自增ID. 1,删除原有主键:ALTER TABLE `table_name` DROP `id`; 2, ...
- mysql数据库表的自增主键号不规律,重新排列
mysql数据库表的自增主键ID乱了,需要重新排序. 原理:删除原有的自增ID,重新建立新的自增ID. 1.删除原有主键: ALTER TABLE `table_name` DROP `id`; 2. ...
- oracle如何创建表的自增ID(通过触发器)
Oracle中创建表的自增ID(通过触发器),序列的自增ID和触发器的自增ID的区别 1.新增数据(序列) --创建示例表 -- create table Student( stuId ) not n ...
- 如何在MySQl数据库中给已有的数据表添加自增ID?
由于使用MySQL数据库还没有多久的缘故,在搭建后台往数据库导入数据的时候发现新增的表单是没有自增id的,因次就有了上面这个问题. 解决方法 1.给某一张表先增加一个字段,这里我们就以node_tab ...
- SQL SERVER 从其它数据库中复制带自增ID主键的表数据
SQL SERVER两个结构相同(或不同)的表,互相导入数据,方法有两种: 1.使用SQL SERVER 自带的导出.导入功能,在库名上右击,“任务”,导出数据.导入数据,这个操作具体不就不多讲了. ...
- SQL Server数据库表重置自增主键号(通常是指ID)
执行 DBCC CHECKIDENT ('table_name', NORESEED) 以确定列中的当前最大值 然后使用 DBCC CHECKIDENT ('table_name', RESEED,n ...
- oracle中如何创建表的自增ID(通过序列)
1.什么是序列呢? 序列是一数据库对象,利用它可生成唯一的整数.一般使用序列自动地生成主码值.一个序列的值是由特别的Oracle程序自动生成,因而序列避免了在运用层实现序列而引起的性能瓶颈. Orac ...
- sqlserver为数据库表增加自增字段
需求: 数据库为SQLServer.对已有的数据库表customer加一个序号字段,一次性对所有现存客户加上编号,并在新建客户时自动增加一个编号,数值自增1. 解决方法: 1. 复制表结构.把原 ...
随机推荐
- linux 磁盘管理命令之df-(转自 Howie的专栏)
linux中df命令的功能是用来检查linux服务器的文件系统的磁盘空间占用情况.可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息. 1.命令格式: df [选项] [文件] 2.命 ...
- 『动善时』JMeter基础 — 33、JMeter察看结果树的显示模式详解
目录 1.CSS Selector Tester视图 2.HTML查看器 (1)HTML视图 (2)HTML(download resources)视图 (3)HTML Source Formatte ...
- checkbox,select,radio 选取值,设定值,回显值
获取一组radio被选中项的值var item = $('input[@name=items][@checked]').val();获取select被选中项的文本var item = $(" ...
- 字符识别OCR原理及应用实现
字符识别OCR原理及应用实现 文本是人类最重要的信息来源之一,自然场景中充满了形形色色的文字符号.光学字符识别(OCR)相信大家都不陌生,就是指电子设备(例如扫描仪或数码相机)检查纸上打印的字符,通过 ...
- FPGA与ASIC:它们之间的区别以及使用哪一种?
FPGA与ASIC:它们之间的区别以及使用哪一种? FPGA Vs ASIC: Differences Between Them And Which One To Use? VL82C486 Sing ...
- mybatis——一级缓存、二级缓存
一.Mybatis缓存 ● MyBatis包含一个非常强大的查询緩存特性,它可以非常方便地定制和配置缓存.绶存可以极大的提升查询效率. ● MyBatis系统中默认定义了两级缓存:一级缓存和二级缓存 ...
- 【渗透实战】记一次艰难的内网漫游第四期_蹭我WIFI?看我如何利用组合拳日进蹭网者内网
/文章作者:Kali_MG1937 CSDN博客ID:ALDYS4 QQ:3496925334/ 内网漫游系列第三期:[渗透实战]记一次艰难的内网漫游第三期_我是如何利用APT攻击拿到内网最高权限的 ...
- 【NX二次开发】Block UI对话框-代码生成部分
常规: 语言:生成的代码语言 生成附注:是否生成注释代码 生成特定与块的代码: 输入点: 回调:对话框通过回调函数调用,例如通过另一个对话框的按钮调用本对话框 菜单:对话框通过菜单调用 用户出口:对话 ...
- 痞子衡嵌入式:超级下载算法(RT-UFL)开发笔记番外(1) - JLinkScript妙用
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是超级下载算法开发笔记番外篇之JLinkScript妙用. JLinkScript 文件是配套 J-Link 调试器使用的脚本,这个脚本适 ...
- MySQL的Limit 性能差?真的不能再用了?
首先说明一下MySQL的版本: mysql> select version();+-----------+| version() |+-----------+| 5.7.17 |+------- ...