Detailed explanation of Spring data instances that define default time and date
Preface:
The requirements are as follows:
1. The creation time and update time can only be generated by the database and are not allowed to be generated in the entity class, because the time/time zone of each node may not be always the same. In addition, prevent artificial insertion of custom time.
2. Create the default time when inserting the record. The creation time cannot be empty. Once the time is inserted, it is not allowed to be modified in the entity class in the future.
3. The default update log field after the record is created is null, which means that the record has not been modified. Once the data is modified, the Modify Date field will record the last modification time.
4. You can even implement a history table through triggers to record historical modifications of data. For details, please refer to the author's other e-book "Netkiller Architect Notes" related to database design chapters.
10.1.6. Default time rules
10.1.6.1. CreatedDate
Spring provides import;
But these can only work on entity classes.
@CreatedDate private Date createdDateTime;
10.1.6.3. Default creation date and time definition at the database level
package ; import ; import ; import ; import ; import ; @Entity @Table public class ElasticsearchTrash { @Id private int id; @Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP") private Date ctime; public int getId() { return id; } public void setId(int id) { = id; } public Date getCtime() { return ctime; } public void setCtime(Date ctime) { = ctime; } }
Corresponding database DDL
CREATE TABLE `elasticsearch_trash` ( `id` int(11) NOT NULL, `ctime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
10.1.6.4. Definition of default creation date and update time at the database level
The requirements are as follows:
1. The creation time and update time can only be generated by the database and are not allowed to be generated in the entity class, because the time/time zone of each node may not be always the same. In addition, prevent artificial insertion of custom time.
2. Create the default time when inserting the record. The creation time cannot be empty. Once the time is inserted, it is not allowed to be modified in the entity class in the future.
3. The default update log field after the record is created is null, which means that the record has not been modified. Once the data is modified, the Modify Date field will record the last modification time.
4. You can even implement a history table through triggers to record historical modifications of data. For details, please refer to the author's other e-book "Netkiller Architect Notes" related to database design chapters.
package ; import ; import ; import ; import ; import ; import ; @Entity @Table public class ElasticsearchTrash { @Id private int id; // Creation time @Column(insertable = false, updatable = false, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP") private Date ctime; // Modify time @Column(nullable = true, insertable = false, updatable = false, columnDefinition = "TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP") private Date mtime; public int getId() { return id; } public void setId(int id) { = id; } public Date getCtime() { return ctime; } public void setCtime(Date ctime) { = ctime; } public Date getMtime() { return mtime; } public void setMtime(Date mtime) { = mtime; } }
Corresponding database DDL
CREATE TABLE `elasticsearch_trash` ( `id` int(11) NOT NULL, `ctime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `mtime` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8
10.1.6.5. Last modified time
Requirements: Record the last modification time
package ; import ; import ; import ; import ; import ; @Entity @Table public class ElasticsearchTrash { @Id private int id; @Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP") private Date lastModified; }
The DDL statement is as follows
CREATE TABLE `elasticsearch_trash` ( `id` int(11) NOT NULL, `ctime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The above is an example of Spring data defining the default time and date. If you have any questions, please leave a message or go to the community of this site to communicate and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!