日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring or hibernate saveOrUpdateAll 偶然失效

發布時間:2025/3/20 javascript 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring or hibernate saveOrUpdateAll 偶然失效 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近遇到的一個問題

當我們使用hibernatetemplate的saveOrUpdateAll 方法,在數據量達到一定程度時,會必然出現如下的錯誤:

“identifier of an instance altered from X to Y”?

這是很奇怪的,

問題【1】該方法實現是這個樣子的:

public void saveOrUpdateAll(final Collection entities) throws DataAccessException { 768 executeWithNativeSession(new HibernateCallback() { 769 public Object doInHibernate(Session session) throws HibernateException { 770 checkWriteOperationAllowed(session); 771 for (Iterator it = entities.iterator(); it.hasNext();) { 772 session.saveOrUpdate(it.next()); 773 } 774 return null; 775 } 776 }); 777 }

如果真的是這樣的話,為什么還會出現上述的錯誤?

從上面的方法看就是一個一個的迭代執行,而這樣與自己迭代執行有什么區別? 為什么這個會報錯?


問題【2】經測試,單獨一次提交1000,提交多次 是不會出現該問題得。這是問什么?


性能方面1w條-》152s 確實較慢 后面試一下直接使用jdbc卡看需要多久。

TO BE CONTINUED .....[代碼隨后附上]

https://github.com/gaoxingliang/algintroduction/tree/master/src/hibernate


update:hibernate4 兼容性有點問題,沒有找到updateAll方法,可能正如下面的鏈接中說說的可能已經刪除了。

下面這些鏈接會有幫助:

http://stackoverflow.com/questions/4179166/hibernate-how-to-fix-identifier-of-an-instance-altered-from-x-to-y

http://forum.spring.io/forum/spring-projects/data/56941-problem-with-saveorupdateall-method ?這里說了如何替代該方法,就是在迭代器中單獨調用saveOrUpdate


http://forum.spring.io/forum/spring-projects/data/113874-hibernatetemplate-saveorupdateall-collection-entities-performance 這里描述它的性能,2000/15s 與實際一致。

http://grepcode.com/file/repo1.maven.org/maven2/org.springframework/spring-orm/2.5.6/org/springframework/orm/hibernate3/HibernateTemplate.java#767


PS:

從這里下載打好的spring zip包:http://maven.springframework.org/release/org/springframework/spring/3.2.4.RELEASE/

hibernate3:http://sourceforge.net/projects/hibernate/files/hibernate3/3.5.0-Final/hibernate-distribution-3.5.0-Final-dist.zip/download?use_mirror=nchc




轉載于:https://www.cnblogs.com/scugxl/p/4131791.html

總結

以上是生活随笔為你收集整理的Spring or hibernate saveOrUpdateAll 偶然失效的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。