Springmvc incorporates spring-data-jPA using audit annotations @createdDate, @lastModifiedDate issues using error issues

The desired answer is

Column ‘create_time’ cannot be null let JPA automatically generate create time and update time for us

Step 1: write the entity class

  • @Column

  • @GeneratedValue(strategy= GenerationType.IDENTITY)

  • @Id

  • @mappedsuperClass. (Add the annotation as appropriate) for the parent class. This notation is added when the class is definitely a parent. If you change it to @entity, then after inheritance, multiple classes will inherit and only one table will be generated, instead of multiple inheritance, multiple tables will be generated.

  • EntityListeners(AuditListener.class) track changes in the properties of entities. They provide statuses before saving, after saving, before updating, after updating, before deleting, and after deleting. Like interceptors, you can override personalization logic in interceptors.

  • @creationTIMESTAMP Using this annotation allows Hibernate to create a default value at insert time for the date type of the attribute of the annotation.

  • @updateTIMESTAMP Using this annotation allows Hibernate to create a default value for the date type of the annotation’s attribute at update time.


/ * * *@program: BaseEntity
 * @description: Base entity public attribute *@author: hewo
 * @create: the 2020-12-20 battle overtook * * /
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public class BaseEntity implements Serializable {
    / * * * * /
    private static final long serialVersionUID = 1L;

    @Column(name="id",nullable=false,length=11)
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Id
    private Long id;/ / the only id

    @Column(name="create_time",nullable=false)
    @CreationTimestamp
    @CreatedDate
    private Date createTime;// Create time

    @Column(name="update_time",nullable=false)
    @UpdateTimestamp
    @LastModifiedDate
    private Date updateTime;// Update time
    
    // Getters and setters omit...
Copy the code

/ * * *@program: OperaterLog
 * @description: Log entity class *@author: hewo
 * @create: however, * * / 2020-12-20
@Entity
@Table(name = "zc_operater_log")
@EntityListeners(AuditingEntityListener.class)
public class OperaterLog extends BaseEntity {

    @Column(name = "operater",nullable = false,length = 11)
    private String operater;

    @Column(name = "content",nullable = false,length = 128)
    private String content;

    public String getOperater(a) {
        return operater;
    }

    public void setOperater(String operater) {
        this.operater = operater;
    }

    public String getContent(a) {
        return content;
    }

    public void setContent(String content) {
        this.content = content; }}Copy the code

Step 2 Addspring-aspects.jarRely on

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>${spring.version}</version>
</dependency>
Copy the code

The third step is to add springmVC-config.xml to the configuration fileJpa audit

<! - the jpa audit - >
<jpa:auditing />
Copy the code

😀😀😀😀😀😀😀😀