Spring data defines instances of default time and date, springdata

Source: Internet
Author: User

Spring data defines instances of default time and date, springdata

Explain how Spring data defines instances of default time and date

Preface:

The requirement is as follows:

1. The creation time and update time can only be generated by the database and cannot be generated in the Entity class, because the time/Time Zone of each node may not always be. In addition, the User-Defined time is prevented from being inserted manually.

2. The default time is created when a record is inserted. The creation time cannot be blank. The time cannot be modified in the object class after it is inserted.

3. When the log field is updated after the record is created, the default value is null, indicating that the record has not been modified. Once the data is modified, the last modification time of the date field is recorded.

4. You can even use a trigger to implement a history table to record historical changes to the data. For details, see the database design section of the author's other ebook Netkiller impact ect.

10.1.6. Default time rule

10.1.6.1. CreatedDate

Spring provides import org. springframework. data. annotation. CreatedDate;

However, these actions can only be applied to entity classes.

@CreatedDate  private Date createdDateTime;

10.1.6.3. Database-level default creation date and time definition

package cn.netkiller.api.domain.elasticsearch;import java.util.Date;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.Table;@Entity@Tablepublic 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) { this.id = id; } public Date getCtime() { return ctime; } public void setCtime(Date ctime) { this.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. Database-level default creation date and update time definition

The requirement is as follows:

1. The creation time and update time can only be generated by the database and cannot be generated in the Entity class, because the time/Time Zone of each node may not always be. In addition, the User-Defined time is prevented from being inserted manually.

2. The default time is created when a record is inserted. The creation time cannot be blank. The time cannot be modified in the object class after it is inserted.

3. When the log field is updated after the record is created, the default value is null, indicating that the record has not been modified. Once the data is modified, the last modification time of the date field is recorded.

4. You can even use a trigger to implement a history table to record historical changes to the data. For details, see the database design section of the author's other ebook Netkiller impact ect.

Package cn. netkiller. api. domain. elasticsearch; import java. util. date; import javax. persistence. column; import javax. persistence. entity; import javax. persistence. id; import javax. persistence. table; import javax. validation. constraints. null; @ Entity @ Tablepublic class ElasticsearchTrash {@ Id private int id; // creation time @ Column (insertable = false, updatable = false, columnDefinition = "timestamp default CURRENT_TIMESTAMP ") private Date ctime; // modification 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) {this. id = id;} public Date getCtime () {return ctime;} public void setCtime (Date ctime) {this. ctime = ctime;} public Date getMtime () {return mtime;} public void setMtime (Date mtime) {this. 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 modification time

Requirement: record the last modification time

package cn.netkiller.api.domain.elasticsearch;import java.util.Date;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.Table;@Entity@Tablepublic class ElasticsearchTrash { @Id private int id; @Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP") private Date lastModified;}

The DDL statements are 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 on this site for discussion. Thank you for reading this article and hope to help you. Thank you for your support for this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.