您好,登錄后才能下訂單哦!
這篇文章主要介紹“如何解決@DynamicUpdate //自動更新updatetime的問題”,在日常操作中,相信很多人在如何解決@DynamicUpdate //自動更新updatetime的問題問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何解決@DynamicUpdate //自動更新updatetime的問題”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
在數(shù)據(jù)庫中的字節(jié),包括updatetime,但是我更新某一個內(nèi)容時,updatetime,沒有自動更新,這時候我們只需要在data類中加上注解 @DynamicUpdate 動態(tài)更新的意思
@Entity @DynamicUpdate //自動更新(動態(tài)更新)updatetime public class ProductCategory { /*類目Id*/ @Id //主鍵 @GeneratedValue(strategy = GenerationType.IDENTITY) //子增類型 private Integer categoryId; /*類目名字*/ private String categoryName; /*類目編號*/ private Integer categoryType; /*創(chuàng)建時間*/ 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 + '}'; } }
平時在寫業(yè)務(wù)時, 會涉及到某條數(shù)據(jù)的更新。 當(dāng)我們使用hibernate的 this.getCurrentSession().saveOrUpdate(o) 更新對象時,會默認(rèn)的更新對象(o)所有的字段,包括屬性為null和未修改的字段也會更新到原有的數(shù)據(jù)庫表中。
造成了原有的數(shù)據(jù)丟失或數(shù)據(jù)重復(fù)修改。
通常這情況下我們所希望的是僅更新對象(o)中修改過且有值的字段,此時就需要用到@DynamicUpdate注解。
標(biāo)注位置: 實體映射類上
根據(jù)官方接口文檔所說,如果我們在使用該注解時必須同時使用 @SelectBeforeUpdate 注解表明在更新前先進行查詢操作。否則是即使聲明該注解也是無效的。
也就是說當(dāng)我們要更新的對象不在session會話的管理中,無法比對哪個字段需要更新,則所標(biāo)注的注解無效。故需要在其更新前進行查詢,此時當(dāng)前數(shù)據(jù)已經(jīng)在sessio的會話中,即可動態(tài)更新數(shù)據(jù)。
到此,關(guān)于“如何解決@DynamicUpdate //自動更新updatetime的問題”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。