Buggy code:
public class DetachedStateManager extends AttachStrategy implements Serializable , OpenJPAStateManager { private static final long serialVersionUID = 4112223665584731100L ; private static final Localizer _loc = Localizer . forPackage ( DetachedStateManager . class ) ; private final PersistenceCapable _pc ; private final boolean _embedded ; private final boolean _access ; private final BitSet _loaded ; private final BitSet _dirty ; private final Object _oid ; private final Object _version ; private final ReentrantLock _lock ; public DetachedStateManager ( PersistenceCapable pc , OpenJPAStateManager sm , BitSet load , boolean access , boolean multithreaded ) { } public Object attach ( AttachManager manager , Object toAttach , ClassMetaData meta , PersistenceCapable into , OpenJPAStateManager owner , ValueMetaData ownerMeta , boolean explicit ) { BrokerImpl broker = manager . getBroker ( ) ; StateManagerImpl sm ; if ( _embedded ) { if ( ( _dirty . length ( ) ) > 0 ) owner . dirty ( ownerMeta . getFieldMetaData ( ) . getIndex ( ) ) ; sm = ( ( StateManagerImpl ) ( broker . embed ( _pc , _oid , owner , ownerMeta ) ) ) ; ImplHelper . toPersistenceCapable ( toAttach , broker . getConfiguration ( ) ) . pcReplaceStateManager ( this ) ; } else { PCState state = ( ( _dirty . length ( ) ) > 0 ) ? PCState . PDIRTY : PCState . PCLEAN ; sm = ( ( StateManagerImpl ) ( broker . copy ( this , state ) ) ) ; } PersistenceCapable pc = sm . getPersistenceCapable ( ) ; manager . setAttachedCopy ( toAttach , pc ) ; manager . fireBeforeAttach ( toAttach , meta ) ; <START_BUG> FieldMetaData [ ] fields = meta . getFields ( ) ; <END_BUG> int restore = broker . getRestoreState ( ) ; if ( ( _dirty . length ( ) ) > 0 ) { BitSet load = new BitSet ( fields . length ) ; for ( int i = 0 ; i < ( fields . length ) ; i ++ ) { if ( ! ( _dirty . get ( i ) ) ) continue ; switch ( fields [ i ] . getDeclaredTypeCode ( ) ) { case JavaTypes . ARRAY : case JavaTypes . COLLECTION : if ( ( restore == ( RestoreState . RESTORE_ALL ) ) || ( ( fields [ i ] . getElement ( ) . getCascadeDelete ( ) ) == ( ValueMetaData . CASCADE_AUTO ) ) ) load . set ( i ) ; break ; case JavaTypes . MAP : if ( ( ( restore == ( RestoreState . RESTORE_ALL ) ) || ( ( fields [ i ] . getElement ( ) . getCascadeDelete ( ) ) == ( ValueMetaData . CASCADE_AUTO ) ) ) || ( ( fields [ i ] . getKey ( ) . getCascadeDelete ( ) ) == ( ValueMetaData . CASCADE_AUTO ) ) ) load . set ( i ) ; break ; default : if ( ( restore != ( RestoreState . RESTORE_NONE ) ) || ( ( fields [ i ] . getCascadeDelete ( ) ) == ( ValueMetaData . CASCADE_AUTO ) ) ) load . set ( i ) ; } } FetchConfiguration fc = broker . getFetchConfiguration ( ) ; sm . loadFields ( load , fc , fc . getWriteLockLevel ( ) , null ) ; } Object origVersion = sm . getVersion ( ) ; sm . setVersion ( _version ) ; BitSet loaded = sm . getLoaded ( ) ; int set = StateManager . SET_ATTACH ; for ( int i = 0 ; i < ( fields . length ) ; i ++ ) { if ( ! ( _loaded . get ( i ) ) ) continue ; if ( ( ( ! ( _dirty . get ( i ) ) ) && ( loaded . get ( i ) ) ) && ( DetachedStateManager . ignoreLoaded ( fields [ i ] ) ) ) continue ; provideField ( i ) ; switch ( fields [ i ] . getDeclaredTypeCode ( ) ) { case JavaTypes . BOOLEAN : if ( _dirty . get ( i ) ) sm . settingBooleanField ( pc , i , ( ( loaded . get ( i ) ) && ( sm . fetchBooleanField ( i ) ) ) , ( ( longval ) == 1 ) , set ) ; else sm . storeBooleanField ( i , ( ( longval ) == 1 ) ) ; break ; case JavaTypes . BYTE : if ( _dirty . get ( i ) ) sm . settingByteField ( pc , i , ( ! ( loaded . get ( i ) ) ? ( ( byte ) ( 0 ) ) : sm . fetchByteField ( i ) ) , ( ( byte ) ( longval ) ) , set ) ; else sm . storeByteField ( i , ( ( byte ) ( longval ) ) ) ; break ; case JavaTypes . CHAR : if ( _dirty . get ( i ) ) sm . settingCharField ( pc , i , ( ! ( loaded . get ( i ) ) ? ( ( char ) ( 0 ) ) : sm . fetchCharField ( i ) ) , ( ( char ) ( longval ) ) , set ) ; else sm . storeCharField ( i , ( ( char ) ( longval ) ) ) ; break ; case JavaTypes . INT : if ( _dirty . get ( i ) ) sm . settingIntField ( pc , i , ( ! ( loaded . get ( i ) ) ? 0 : sm . fetchIntField ( i ) ) , ( ( int ) ( longval ) ) , set ) ; else sm . storeIntField ( i , ( ( int ) ( longval ) ) ) ; break ;

Fix:
FieldMetaData [ ] fields = sm . getMetaData ( ) . getFields ( ) ;