Buggy code:
public abstract class StoreCollectionFieldStrategy extends ContainerFieldStrategy { protected abstract ForeignKey getJoinForeignKey ( ClassMapping elem ) { } protected abstract void selectElement ( Select sel , ClassMapping elem , JDBCStore store , JDBCFetchConfiguration fetch , int eagerMode , Joins joins ) { } protected abstract Object loadElement ( OpenJPAStateManager sm , JDBCStore store , JDBCFetchConfiguration fetch , Result res , Joins joins ) throws SQLException { } protected abstract Joins joinElementRelation ( Joins joins , ClassMapping elem ) { } protected abstract Joins join ( Joins joins , ClassMapping elem ) { } protected abstract Proxy newLRSProxy ( ) { } protected Collection toCollection ( Object val ) { } protected void add ( JDBCStore store , Object coll , Object obj ) { } private ClassMapping getDefaultElementMapping ( boolean traverse ) { } public int supportsSelect ( Select sel , int type , OpenJPAStateManager sm , JDBCStore store , JDBCFetchConfiguration fetch ) { } public void selectEagerParallel ( SelectExecutor sel , final OpenJPAStateManager sm , final JDBCStore store , final JDBCFetchConfiguration fetch , final int eagerMode ) { } public void selectEagerJoin ( Select sel , OpenJPAStateManager sm , JDBCStore store , JDBCFetchConfiguration fetch , int eagerMode ) { } public boolean isEagerSelectToMany ( ) { } private void selectEager ( Select sel , ClassMapping elem , OpenJPAStateManager sm , JDBCStore store , JDBCFetchConfiguration fetch , int eagerMode , boolean selectOid , boolean outer ) { } public Object loadEagerParallel ( OpenJPAStateManager sm , JDBCStore store , JDBCFetchConfiguration fetch , Object res ) throws SQLException { } private Map processEagerParallelResult ( OpenJPAStateManager sm , JDBCStore store , JDBCFetchConfiguration fetch , Result res ) throws SQLException { } private void setMappedBy ( Object oid , Result res ) { } private void setMappedBy ( Object oid , OpenJPAStateManager sm , Object coll , Result res ) { } private Object getNextObjectId ( ClassMapping owner , JDBCStore store , Result res , Object oid ) throws SQLException { } public void loadEagerJoin ( OpenJPAStateManager sm , JDBCStore store , JDBCFetchConfiguration fetch , Result res ) throws SQLException { } private Object getNextRef ( ClassMapping mapping , JDBCStore store , Result res , Object ref , Joins refJoins ) throws SQLException { } public void load ( final OpenJPAStateManager sm , final JDBCStore store , final JDBCFetchConfiguration fetch ) throws SQLException { if ( field . isLRS ( ) ) { Proxy coll = newLRSProxy ( ) ; if ( ( field . getOrderColumn ( ) ) != null ) { Select sel = store . getSQLFactory ( ) . newSelect ( ) ; sel . setAggregate ( true ) ; <START_BUG> StringBuffer sql = new StringBuffer ( ) ; <END_BUG> sql . append ( "MAX(" ) . append ( sel . getColumnAlias ( field . getOrderColumn ( ) ) ) . append ( ")" ) ; sel . select ( sql . toString ( ) , field ) ; ClassMapping rel = getDefaultElementMapping ( false ) ; sel . whereForeignKey ( getJoinForeignKey ( rel ) , sm . getObjectId ( ) , field . getDefiningMapping ( ) , store ) ; Result res = sel . execute ( store , fetch ) ; try { res . next ( ) ; coll . getChangeTracker ( ) . setNextSequence ( ( ( res . getInt ( field ) ) + 1 ) ) ; } finally { res . close ( ) ; } } sm . storeObjectField ( field . getIndex ( ) , coll ) ; return ; } final ClassMapping [ ] elems = getIndependentElementMappings ( true ) ; final Joins [ ] resJoins = new Joins [ Math . max ( 1 , elems . length ) ] ; Union union = store . getSQLFactory ( ) . newUnion ( Math . max ( 1 , elems . length ) ) ; union . select ( new Union . Selector ( ) { public void select ( Select sel , int idx ) { ClassMapping elem = ( ( elems . length ) == 0 ) ? null : elems [ idx ] ; resJoins [ idx ] = selectAll ( sel , elem , sm , store , fetch , EAGER_PARALLEL ) ; } } ) ; Object coll ; ChangeTracker ct = null ; if ( ( field . getTypeCode ( ) ) == ( JavaTypes . ARRAY ) ) coll = new ArrayList ( ) ; else { coll = sm . newProxy ( field . getIndex ( ) ) ; if ( coll instanceof Proxy ) ct = ( ( Proxy ) ( coll ) ) . getChangeTracker ( ) ; } Result res = union . execute ( store , fetch ) ; try { int seq = - 1 ; while ( res . next ( ) ) { if ( ( ct != null ) && ( ( field . getOrderColumn ( ) ) != null ) ) seq = res . getInt ( field . getOrderColumn ( ) ) ; setMappedBy ( sm . getObjectId ( ) , sm , coll , res ) ; add ( store , coll , loadElement ( sm , store , fetch , res , resJoins [ res . indexOf ( ) ] ) ) ; } if ( ( ct != null ) && ( ( field . getOrderColumn ( ) ) != null ) ) ct . setNextSequence ( ( seq + 1 ) ) ; } finally { res . close ( ) ; } if ( ( field . getTypeCode ( ) ) == ( JavaTypes . ARRAY ) ) sm . storeObject ( field . getIndex ( ) , JavaTypes . toArray ( ( ( Collection ) ( coll ) ) , field . getElement ( ) . getType ( ) ) ) ; else sm . storeObject ( field . getIndex ( ) , coll ) ; }

Fix:
StringBuilder sql = new StringBuilder ( ) ;