Spring-Data-Jpa AuditingEntityListener @CreatedDate @LastModifiedDate 用法
2018-02-28 09:46
573 查看
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import org.hibernate.validator.constraints.NotBlank; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.LastModifiedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; import javax.persistence.*; import java.util.Date; /** * */ @Entity @Table(name = "notes") @EntityListeners(AuditingEntityListener.class) @JsonIgnoreProperties(value = {"createdAt", "updatedAt"}, allowGetters = true) public class Note { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @NotBlank private String title; @NotBlank private String content; @Column(nullable = false, updatable = false) @Temporal(TemporalType.TIMESTAMP) @CreatedDate private Date createdAt; @Column(nullable = false) @Temporal(TemporalType.TIMESTAMP) @LastModifiedDate private Date updatedAt; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Date getCreatedAt() { return createdAt; } public void setCreatedAt(Date createdAt) { this.createdAt = createdAt; } public Date getUpdatedAt() { return updatedAt; } public void setUpdatedAt(Date updatedAt) { this.updatedAt = updatedAt; } }
如上示例代码,@EntityListeners(AuditingEntityListener.class) 是用于监听实体类添加或者删除操作的。@JsonIgnoreProperties(value = {"createdAt", "updatedAt"},
allowGetters = true) 这个注解是用于在除了在获取createAt,updateAt属性时进行操作,其他创建和更新操作都由jpa完成@Column(nullable = false, updatable = false) 其中updatable = false表示不进行更新操作@CreatedDate 表示该字为创建时间字段,在这个实体被insert时,设置值;@LastModifiedDate同理
相关文章推荐
- Spring JPA 使用@CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy 自动生成时间和修改者
- JPA Override 源码中createdDate,createdBy,lastModifiedBy,lastModifiedDate(过于麻烦,见下一篇已更新方法)
- Spring data mongodb @CreatedBy@LastModifiedBy@CreatedBy@LastModifiedBy SpringSecurityAuditorAware,只记录用户名
- Jpa配置实体类创建时间更新时间自动赋值,@CreateDate,@LastModifiedDate
- Jpa配置实体类创建时间更新时间自动赋值,@CreateDate,@LastModifiedDate
- spring data jpa auditing
- @Query注解的用法(Spring Data JPA)
- @Query注解的用法(Spring Data JPA)
- Spring JPA 使用@CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy 自动生成时间和修改者
- @Query注解的用法(Spring Data JPA)
- @Query注解的用法(Spring Data JPA)
- @Query注解的用法(Spring Data JPA)
- IntelliJ IDEA can't resolve entity in spring data jpa @query annotation 报红
- Spring Data Jpa @Query Pageable用法
- @Query注解的用法(Spring Data JPA)
- Spring-data-jpa 之Specification in的用法
- spring data jpa懒加载session失效:OpenEntityManagerInViewFilter 取代OpenSessionInViewFilter
- Spring Data审计功能@CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy的使用
- Spring Data JPA Entity Repository 多路径配置
- @Query注解的用法(Spring Data JPA)