1 回答

TA貢獻1842條經(jīng)驗 獲得超13個贊
再次檢查您的問題,我認(rèn)為罪魁禍?zhǔn)资荋ibernate.initialize. 因為它修改了傳遞的對象。
這是該方法的源代碼。正如我們清楚地看到的,它修改了它們的狀態(tài),并且因為我們知道ConcurrentModificationException在迭代時內(nèi)容被更改時會發(fā)生這種情況,這顯然是原因。
public static void initialize(Object proxy) throws HibernateException {
if ( proxy == null ) {
return;
}
if ( proxy instanceof HibernateProxy ) {
( (HibernateProxy) proxy ).getHibernateLazyInitializer().initialize();
}
else if ( proxy instanceof PersistentCollection ) {
( (PersistentCollection) proxy ).forceInitialization();
}
else if ( proxy instanceof PersistentAttributeInterceptable ) {
final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) proxy;
final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor();
if ( interceptor instanceof EnhancementAsProxyLazinessInterceptor ) {
( (EnhancementAsProxyLazinessInterceptor) interceptor ).forceInitialize( proxy, null );
}
}
}
當(dāng)使用常規(guī)的 for 循環(huán)時,它一次從集合中訪問一個對象,因此它不會出現(xiàn)異常。
添加回答
舉報