Buggy code:
} } else if ( "null" . equalsIgnoreCase ( targetNameStr ) ) constant = true ; else { QualifiedDBIdentifier path = QualifiedDBIdentifier . getPath ( targetName ) ; fullTarget = ! ( DBIdentifier . isNull ( path . getObjectTableName ( ) ) ) ; if ( ( ! ( DBIdentifier . isNull ( path . getObjectTableName ( ) ) ) ) && ( DBIdentifier . isEmpty ( path . getObjectTableName ( ) ) ) ) { if ( ! inverse ) ttable = local ; targetName = path . getIdentifier ( ) . getUnqualifiedName ( ) ; } else if ( ! ( DBIdentifier . isNull ( path . getObjectTableName ( ) ) ) ) { ttable = MappingInfo . findTable ( context , path . getObjectTableName ( ) , foreign , local , ( inverse ? cls : rel ) ) ; targetName = path . getIdentifier ( ) . getUnqualifiedName ( ) ; } } } if ( ( ttable == local ) && ( local != foreign ) ) { if ( fullName ) throw new org . apache . openjpa . util . MetaDataException ( MappingInfo . _loc . get ( ( prefix + "-bad-fktarget-inverse" ) , new Object [ ] { context , name , foreign , ttable } ) ) ; local = foreign ; foreign = ttable ; } else if ( ttable != null ) { foreign = ttable ; } inverse = ( inverse || ( local != table ) ) || ( ( local == foreign ) && ( ( fullName && ( ! fullTarget ) ) || ( ( DBIdentifier . isNull ( name ) ) && fullTarget ) ) ) ; if ( ( ( ! inversable ) && ( ! constant ) ) && inverse ) { if ( local == foreign ) throw new org . apache . openjpa . util . MetaDataException ( MappingInfo . _loc . get ( ( prefix + "-bad-fk-self-inverse" ) , context , local ) ) ; throw new org . apache . openjpa . util . MetaDataException ( MappingInfo . _loc . get ( ( prefix + "-bad-fk-inverse" ) , context , local , table ) ) ; } if ( ( DBIdentifier . isNull ( name ) ) && constant ) throw new org . apache . openjpa . util . MetaDataException ( MappingInfo . _loc . get ( ( prefix + "-no-fkcol-name-adapt" ) , context ) ) ; if ( ( DBIdentifier . isNull ( name ) ) && ( DBIdentifier . isNull ( targetName ) ) ) { PrimaryKey pk = foreign . getPrimaryKey ( ) ; if ( ( ( ( joins . length ) != 1 ) || ( pk == null ) ) || ( ( pk . getColumns ( ) . length ) != 1 ) ) throw new org . apache . openjpa . util . MetaDataException ( MappingInfo . _loc . get ( ( prefix + "-no-fkcol-name-adapt" ) , context ) ) ; targetName = pk . getColumns ( ) [ 0 ] . getIdentifier ( ) ; } else if ( ( ! ( DBIdentifier . isNull ( name ) ) ) && ( DBIdentifier . isNull ( targetName ) ) ) { PrimaryKey pk = foreign . getPrimaryKey ( ) ; if ( ( ( ( joins . length ) == 1 ) && ( pk != null ) ) && ( ( pk . getColumns ( ) . length ) == 1 ) ) { targetName = pk . getColumns ( ) [ 0 ] . getIdentifier ( ) ; } else if ( ( foreign . getColumn ( name ) ) != null ) { targetName = name ; } else { throw new org . apache . openjpa . util . MetaDataException ( MappingInfo . _loc . get ( ( prefix + "-no-fkcol-target-adapt" ) , context , name ) ) ; } } Column tmplate = new Column ( ) ; tmplate . setIdentifier ( name ) ; if ( ! constant ) { <START_BUG> Column tcol = foreign . getColumn ( targetName ) ; <END_BUG> if ( tcol == null ) throw new org . apache . openjpa . util . MetaDataException ( MappingInfo . _loc . get ( ( prefix + "-bad-fktarget" ) , new Object [ ] { context , targetName , name , foreign } ) ) ; if ( DBIdentifier . isNull ( name ) ) tmplate . setIdentifier ( tcol . getIdentifier ( ) ) ; tmplate . setJavaType ( tcol . getJavaType ( ) ) ; tmplate . setType ( tcol . getType ( ) ) ; tmplate . setTypeName ( tcol . getTypeName ( ) ) ; tmplate . setSize ( tcol . getSize ( ) ) ; tmplate . setDecimalDigits ( tcol . getDecimalDigits ( ) ) ; target = tcol ; } else if ( target instanceof String ) tmplate . setJavaType ( STRING ) ; else if ( target instanceof Integer ) tmplate . setJavaType ( INT ) ; else if ( target instanceof Double ) tmplate . setJavaType ( DOUBLE ) ; if ( def != null ) def . populate ( local , foreign , tmplate , target , inverse , idx , joins . length ) ; if ( ! ( DBIdentifier . isNull ( name ) ) ) tmplate . setIdentifier ( name ) ; Column col = MappingInfo . mergeColumn ( context , prefix , tmplate , true , given , local , adapt , fill ) ; joins [ idx ] [ 0 ] = col ; joins [ idx ] [ 1 ] = target ; if ( inverse ) joins [ idx ] [ 2 ] = Boolean . TRUE ; }

Fix:
Column tcol = foreign . getColumn ( targetName , false ) ;