@DynamicUpdate //自動(dòng)更新updatetime的問(wèn)題
@DynamicUpdate //自動(dòng)更新updatetime
在數(shù)據(jù)庫(kù)中的字節(jié),包括updatetime,但是我更新某一個(gè)內(nèi)容時(shí),updatetime,沒(méi)有自動(dòng)更新,這時(shí)候我們只需要在data類中加上注解 @DynamicUpdate 動(dòng)態(tài)更新的意思
@Entity
@DynamicUpdate //自動(dòng)更新(動(dòng)態(tài)更新)updatetime
public class ProductCategory {
/*類目Id*/
@Id //主鍵
@GeneratedValue(strategy = GenerationType.IDENTITY) //子增類型
private Integer categoryId;
/*類目名字*/
private String categoryName;
/*類目編號(hào)*/
private Integer categoryType;
/*創(chuàng)建時(shí)間*/
private Date createTime;
/*更新時(shí)間*/
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 注解使用及注意事項(xiàng)
使用場(chǎng)景
平時(shí)在寫業(yè)務(wù)時(shí), 會(huì)涉及到某條數(shù)據(jù)的更新。 當(dāng)我們使用hibernate的 this.getCurrentSession().saveOrUpdate(o) 更新對(duì)象時(shí),會(huì)默認(rèn)的更新對(duì)象(o)所有的字段,包括屬性為null和未修改的字段也會(huì)更新到原有的數(shù)據(jù)庫(kù)表中。
造成了原有的數(shù)據(jù)丟失或數(shù)據(jù)重復(fù)修改。
通常這情況下我們所希望的是僅更新對(duì)象(o)中修改過(guò)且有值的字段,此時(shí)就需要用到@DynamicUpdate注解。
注解使用
標(biāo)注位置: 實(shí)體映射類上

注意事項(xiàng)

根據(jù)官方接口文檔所說(shuō),如果我們?cè)谑褂迷撟⒔鈺r(shí)必須同時(shí)使用 @SelectBeforeUpdate 注解表明在更新前先進(jìn)行查詢操作。否則是即使聲明該注解也是無(wú)效的。
個(gè)人理解:
也就是說(shuō)當(dāng)我們要更新的對(duì)象不在session會(huì)話的管理中,無(wú)法比對(duì)哪個(gè)字段需要更新,則所標(biāo)注的注解無(wú)效。故需要在其更新前進(jìn)行查詢,此時(shí)當(dāng)前數(shù)據(jù)已經(jīng)在sessio的會(huì)話中,即可動(dòng)態(tài)更新數(shù)據(jù)。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java web基礎(chǔ)學(xué)習(xí)之開發(fā)環(huán)境篇(詳解)
eclipse實(shí)現(xiàn)ElGamal數(shù)字簽名
SpringBoot如何配置文件給bean賦值問(wèn)題
教你用java?stream對(duì)集合中的對(duì)象按指定字段進(jìn)行分組并統(tǒng)計(jì)
Java中使用synchronized關(guān)鍵字實(shí)現(xiàn)簡(jiǎn)單同步操作示例
redis分布式鎖RedissonLock的實(shí)現(xiàn)細(xì)節(jié)解析
關(guān)于MyBatis 查詢數(shù)據(jù)時(shí)屬性中多對(duì)一的問(wèn)題(多條數(shù)據(jù)對(duì)應(yīng)一條數(shù)據(jù))

