基于@LastModifiedDate不起作用的解决方案
作者:谁把我名字用了!
@LastModifiedDate不起作用
在实体中添加注解 @EntityListeners(AuditingEntityListener.class)监听实体变化
在自动更新时间戳字段增加 @LastModifiedDate
在Spring boot启动类增加注解 @EnableJpaAuditing启用JPA审计(自动填充默认值)
如果你是使用JPA的save(实体)方法去更新数据是没有问题的,如果是使用SQL/JPQL语句就会失效。
比如:
@Query("update xxx set x = ? where x = ?")这里提供最简单的解决办法,语句里时间字段赋值CURRENT_TIMESTAMP即可。
JPA中@CreatedDate和@LastModifiedDate的使用
前些时间写了新项目,然后尝试使用了Spring Data JPA,发现新世界。很多功能都可以基于注解实现,为开发省去了不少功夫。
关于时间的生成注解@CreatedDate和@LastModifiedDate的使用,在此记录一下。
使用步骤
1.在实体类上加上注解 @EntityListeners(AuditingEntityListener.class),在相应的字段上添加对应的时间注解 @LastModifiedDate 和 @CreatedDate。
注意:日期的类型可以使用Date,也可以使用Long。我一般习惯用Date。
//@Data lombok注解,替我们生成getter和setter。
@Data
@Entity
@Table(name = "task")
@EntityListeners(AuditingEntityListener.class)
public class Task {
/**
* 自增主键
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
/**
* 创建时间
*/
@CreatedDate
@Column(name = "createTime", columnDefinition = "timestamp not null default current_timestamp")
private Date createTime;
/**
* 更新时间
*/
@LastModifiedDate
@Column(name = "updateTime", columnDefinition = "timestamp not null default current_timestamp")
private Date updateTime;
}2.在Application启动类中添加注解 @EnableJpaAuditing。
@EnableJpaAuditing
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}3.除了上面提到的注解外,Spring Data JPA 还提供 @CreatedBy 和 @LastModifiedBy 注解,用于保存和更新当前操作用户的信息(如id、name)。
如果有这方面的需求,可以参考下面的配置实现
代码如下:
@Data
@Entity
@EntityListeners(AuditingEntityListener.class)
public class Task {
/**
* 自增主键
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
/**
* 创建时间
*/
@CreatedDate
@Column(name = "createTime", columnDefinition = "timestamp not null default current_timestamp")
private Date createTime;
/**
* 更新时间
*/
@LastModifiedDate
@Column(name = "updateTime", columnDefinition = "timestamp not null default current_timestamp")
private Date updateTime;
/**
* 创建人
*/
@CreatedBy
@Column(name = "createBy", columnDefinition = "varchar(255) not null")
private String createBy;
/**
* 最后修改人
*/
@LastModifiedBy
@Column(name = "lastModifiedBy", columnDefinition = "varchar(255) not null")
private String lastModifiedBy;
}获取操作员信息
/**
* Spring Data JPA通过AuditorAware<T>接口获取用户信息,
* 其中泛型T可以为String保存用户名,也可以为Long/Integer保存用户ID。
* @author EvanWang
*
*/
@Component
public class AuditorConfig implements AuditorAware<String> {
/**
* 返回操作员标志信息
*
* @return
*/
@Override
public Optional<String> getCurrentAuditor() {
// 这里应根据实际业务情况获取具体信息
return Optional.of(userName);
}
}补充注解
Hibernate 也提供了类似上述时间注解的功能实现,这种方法只需要一步配置,更改为注解 @UpdateTimestamp 和 @CreationTimestamp
代码如下:
@Data
@MappedSuperclass
@NoArgsConstructor
@AllArgsConstructor
public class Task {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@UpdateTimestamp
@Column(name = "updateTime", columnDefinition = "timestamp not null default current_timestamp")
private Date updateTime;
@CreationTimestamp
@Column(name = "updateTime", columnDefinition = "timestamp not null default current_timestamp")
private Date createTime;
}总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
