@DynamicUpdate //自动更新updatetime的问题
@DynamicUpdate //自动更新updatetime
在数据库中的字节,包括updatetime,但是我更新某一个内容时,updatetime,没有自动更新,这时候我们只需要在data类中加上注解 @DynamicUpdate 动态更新的意思
@Entity
@DynamicUpdate //自动更新(动态更新)updatetime
public class ProductCategory {
/*类目Id*/
@Id //主键
@GeneratedValue(strategy = GenerationType.IDENTITY) //子增类型
private Integer categoryId;
/*类目名字*/
private String categoryName;
/*类目编号*/
private Integer categoryType;
/*创建时间*/
private Date createTime;
/*更新时间*/
private Date updateTime;
public Integer getCategoryId(int i) {
return categoryId;
}
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public Integer getCategoryType() {
return categoryType;
}
public void setCategoryType(Integer categoryType) {
this.categoryType = categoryType;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
@Override
public String toString() {
return "ProductCategory{" +
"categoryId=" + categoryId +
", categoryName='" + categoryName + '\'' +
", categoryType=" + categoryType +
'}';
}
}
@DynamicUpdate 注解使用及注意事项
使用场景
平时在写业务时, 会涉及到某条数据的更新。 当我们使用hibernate的 this.getCurrentSession().saveOrUpdate(o) 更新对象时,会默认的更新对象(o)所有的字段,包括属性为null和未修改的字段也会更新到原有的数据库表中。
造成了原有的数据丢失或数据重复修改。
通常这情况下我们所希望的是仅更新对象(o)中修改过且有值的字段,此时就需要用到@DynamicUpdate注解。
注解使用
标注位置: 实体映射类上

注意事项

根据官方接口文档所说,如果我们在使用该注解时必须同时使用 @SelectBeforeUpdate 注解表明在更新前先进行查询操作。否则是即使声明该注解也是无效的。
个人理解:
也就是说当我们要更新的对象不在session会话的管理中,无法比对哪个字段需要更新,则所标注的注解无效。故需要在其更新前进行查询,此时当前数据已经在sessio的会话中,即可动态更新数据。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
java日期格式化SimpleDateFormat的使用详解
这篇文章主要介绍了java SimpleDateFormat使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2019-05-05
一文彻底弄懂Java中MultipartFile接口和File类
MultipartFile是一个接口,我们可以理解为是Spring 给我们绑定的一个在使用文件上传等时简便实现的口子,这篇文章主要给大家介绍了关于如何通过一文彻底弄懂Java中MultipartFile接口和File类的相关资料,需要的朋友可以参考下2023-11-11
前端如何调用后端接口进行数据交互详解(axios和SpringBoot)
一般来讲前端不会给后端接口,而是后端给前端接口的情况比较普遍,下面这篇文章主要给大家介绍了关于前端如何调用后端接口进行数据交互的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下2023-03-03
SpringBoot集成Redisson操作Redis的实现方法
Redisson是一个用于Java的Redis客户端,它提供了在分布式环境下操作Redis数据库的简单、高效的方式,本文主要介绍了SpringBoot集成Redisson操作Redis的实现方法,具有一定的参考价值,感兴趣的可以了解一下2024-03-03


最新评论