天天看點

Hibernate防止setter()方法更新資料庫 Hibernate防止setter()方法更新資料庫

版權聲明:本文首發 http://asing1elife.com ,轉載請注明出處。 https://blog.csdn.net/asing1elife/article/details/82796646

Hibernate防止setter()方法更新資料庫

Hibernate從資料庫擷取到對象後直接調用其setter()方法對内部資料做更改,可能會導緻直接将資料更新至資料庫

更多精彩

産生問題的原因

  1. Hibernate分為三種基本狀态:遊離态,自由态,持久态
  2. 從資料庫中擷取到對象屬于持久态,直接進行操作會導緻處于Session中的資料發生改變,進而觸發資料庫更新

解決辦法

  1. 擷取到目前的Session對象,将該對象從Session中清除
super.getEntityDao().getSession().evict(userWork);