org.hibernate.StaleStateException: Batch update returned unexpected row count

轉自http://hi.baidu.com/besun/blog/item/01973ff7a9ce0929730eec38.html

1、a different object with the same identifier value was already associated with the session。

  錯誤原因:在hibernate中同一個session裏面有了兩個相同標識但是是不同實體。
  解決方法一:session.clean()
  PS:如果在clean操作後面又進行了saveOrUpdate(object)等改變數據狀態的操作,有可能會報出"Found two representations of same collection"異常。
  解決方法二:session.refresh(object)
  PS:當object不是數據庫中已有數據的對象的時候,不能使用session.refresh(object)因爲該方法是從 hibernate的session中去重新取object,如果session中沒有這個對象,則會報錯所以當你使用saveOrUpdate (object)之前還需要判斷一下。
  解決方法三:session.merge(object)
  PS:Hibernate裏面自帶的方法,推薦使用。
2、Found two representations of same collection
  錯誤原因:見1。
  解決方法:session.merge(object)
以上兩中異常經常出現在一對多映射和多對多映射中。
3、net.sf.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: BBusinessman

從這個bug的字面上,應該是BBusinessman的某個屬性是一個實體,在這個實體沒有保存之前就保存Businessman對象,導致的錯誤。所以我就一直看Businessman的屬性中到底哪個是實體,結果發現三個,分別City , Type, Status,而且這三個實體都是dao.load( Id , Session)從數據庫中獲得的,不是已經有Id的,或者是null,而不存在沒有保存的實體。

於是我又懷疑,是否是同一個事務需要一個session,如果不使用此session,是不是這個原因,於是我把事務中方法裏所有dao.getSession()這樣不太明確的地方都是用方法傳入的session,這樣session都是同一個了,但是仍舊有此錯誤。

這樣,BBusinessman的各個屬性都是沒有問題的。那麼是否是沒有保存BBusinessman之前,而且BBusinessman又被當作某個實體的屬性,首先保存此實體,然後去保存BBusinessman,這樣是否會導致這個錯誤。結果我發現,正是這個問題。

================錯誤的寫法:===================

Session session = dao.getSession();

Transaction tx = session.beginTransaction();

Bo.setBman( form ,man,session);

Bo.saveChangeTable( man,session); // 把man當作屬性賦給ChangeTable,並保存ChangeTable. 就是這出的錯,

dao.save(man,session); // 保存man

tx.commit();

==================== 應該這樣寫:===============

Session session = dao.getSession();

Transaction tx = session.beginTransaction();

Bo.setBman( form ,man,session);

dao.save(man,session); // 保存man

Bo.saveChangeTable( man,session); // 把man當作屬性賦給ChangeTable,並保存ChangeTable. 就是這出的錯,

tx.commit();

這樣,問題就解決了。

4、Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO or remove 'readOnly' marker from transaction definition 錯誤解決

錯誤代碼:
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO or remove 'readOnly' marker from transaction definition
錯誤原因:
OpenSessionInViewFilter在getSession的時候,會把獲取回來的session的flush mode 設爲FlushMode.NEVER。然後把該sessionFactory綁定到TransactionSynchronizationManager,使request的整個過程都使用同一個session,在請求過後再接除該sessionFactory的綁定,最後closeSessionIfNecessary根據該session是否已和transaction綁定來決定是否關閉session。在這個過程中,若HibernateTemplate 發現自當前session有不是readOnly的transaction,就會獲取到FlushMode.AUTO Session,使方法擁有寫權限。
也即是,如果有不是readOnly的transaction就可以由Flush.NEVER轉爲Flush.AUTO,擁有insert,update,delete操作權限,如果沒有transaction,並且沒有另外人爲地設flush model的話,則doFilter的整個過程都是Flush.NEVER。所以受transaction保護的方法有寫權限,沒受保護的則沒有。
參考文章:
http://calvin.blog.javascud.org/post/46.htm
解決辦法:
採用spring的事務聲明,使方法受transaction控制
<bean id="baseTransaction"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager" ref="transactionManager"/>
<property name="proxyTargetClass" value="true"/>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="userService" parent="baseTransaction">
<property name="target">
<bean class="com.phopesoft.security.service.impl.UserServiceImpl"/>
</property>
</bean>

5、關於Hibernate的 Batch update returned unexpected row count from update異常

ERROR [http-8080-Processor22] (BatchingBatcher.java:60) - Exception executing batch:
org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1

1).使用的是hibernate的saveOrUpdate方法保存實例。saveOrUpdate方法要求ID爲null時才執行SAVE,在其它情況下執行UPDATE。在保存實例的時候是新增,但你的ID不爲null,所以使用的是UPDATE,但是數據庫裏沒有主鍵相關的值,所以出現異常。


=================================================================

異常:
在插入時:
org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1

解決方法:
如果是自增主鍵?
有的數據庫是可以修改自增主鍵例如:mysql,有的數據庫是不允許修改自增主鍵的例如postgresql
不要設置自增主鍵的值


2)

在Hibernate映射一對多,多對一,多對多的時候新增常常會出現這個異常,代碼如下:

public void saveFunctionCell(FunctionCell functionCell, Integer pid) {
System.out.println("現在進行新增操作");
FunctionCell fc = new FunctionCell();
try {
BeanUtils.copyProperties(fc, functionCell);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
fc.setFuncCellID(null);
// 獲得父權限
FunctionCell pfc = functionCellDao.findFunctionCellByID(pid);
fc.setParentFunctionCell(pfc);
functionCellDao.saveFunctionCell(fc);
}

注意特別標識出來的這個地方,BeanUtils拷貝Bean屬性的時候,它會將你的Integer類型全部設置成0,在這裏設置一個空,這樣就不會拋出錯誤了。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章