1 回答

TA貢獻1842條經驗 獲得超13個贊
再次檢查您的問題,我認為罪魁禍首是Hibernate.initialize. 因為它修改了傳遞的對象。
這是該方法的源代碼。正如我們清楚地看到的,它修改了它們的狀態,并且因為我們知道ConcurrentModificationException在迭代時內容被更改時會發生這種情況,這顯然是原因。
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 );
}
}
}
當使用常規的 for 循環時,它一次從集合中訪問一個對象,因此它不會出現異常。
添加回答
舉報