Buggy code:
public class PenaltyHistoryDAOImpl implements PenaltyHistoryDAO { private static Logger logger = LoggerFactory . getLogger ( PenaltyHistoryDAOImpl . class ) ; @ Override public void save ( PenaltyHistory penalty ) { } @ Override public PenaltyHistory get ( Long id ) throws EntityDoesNotExistsException { } private void loadPenaltyHistory ( PenaltyHistory penalty , ResultSet rs ) throws SQLException { } @ Override public List < PenaltyHistory > listByPenaltyId ( Long id ) { } @ Override public PenaltyHistory getLastByPenalty ( Long id ) throws EntityDoesNotExistsException { } @ Override public List < PenaltyHistory > listByPlayer ( Long id , int offset , int limit , int [ ] count ) { <START_BUG> String sqlCount = "SELECT<seq2seq4repair_space>count(h.id)<seq2seq4repair_space>FROM<seq2seq4repair_space>penalty_history<seq2seq4repair_space>h<seq2seq4repair_space>INNER<seq2seq4repair_space>JOIN<seq2seq4repair_space>penalty<seq2seq4repair_space>p<seq2seq4repair_space>ON<seq2seq4repair_space>h.penaltyid<seq2seq4repair_space>=<seq2seq4repair_space>p.id<seq2seq4repair_space>where<seq2seq4repair_space>p.playerid<seq2seq4repair_space>=<seq2seq4repair_space>?<seq2seq4repair_space>GROUP<seq2seq4repair_space>by<seq2seq4repair_space>h.penaltyid" ; <END_BUG> String sql = "SELECT<seq2seq4repair_space>h.*<seq2seq4repair_space>FROM<seq2seq4repair_space>penalty_history<seq2seq4repair_space>h<seq2seq4repair_space>INNER<seq2seq4repair_space>JOIN<seq2seq4repair_space>(SELECT<seq2seq4repair_space>max(h.id)<seq2seq4repair_space>as<seq2seq4repair_space>id<seq2seq4repair_space>FROM<seq2seq4repair_space>penalty_history<seq2seq4repair_space>h<seq2seq4repair_space>INNER<seq2seq4repair_space>JOIN<seq2seq4repair_space>penalty<seq2seq4repair_space>p<seq2seq4repair_space>ON<seq2seq4repair_space>h.penaltyid<seq2seq4repair_space>=<seq2seq4repair_space>p.id<seq2seq4repair_space>where<seq2seq4repair_space>p.playerid<seq2seq4repair_space>=<seq2seq4repair_space>?<seq2seq4repair_space>group<seq2seq4repair_space>by<seq2seq4repair_space>h.penaltyid)<seq2seq4repair_space>hj<seq2seq4repair_space>ON<seq2seq4repair_space>h.id<seq2seq4repair_space>=<seq2seq4repair_space>hj.id<seq2seq4repair_space>ORDER<seq2seq4repair_space>BY<seq2seq4repair_space>h.updated<seq2seq4repair_space>desc<seq2seq4repair_space>limit<seq2seq4repair_space>?,?" ; Connection conn = null ; List < PenaltyHistory > list = new ArrayList < PenaltyHistory > ( ) ; try { conn = ConnectionFactory . getSecondaryConnection ( ) ; PreparedStatement stC = conn . prepareStatement ( sqlCount ) ; stC . setLong ( 1 , id ) ; ResultSet rsC = stC . executeQuery ( ) ; if ( rsC . next ( ) ) { count [ 0 ] = rsC . getInt ( 1 ) ; } PreparedStatement st = conn . prepareStatement ( sql ) ; st . setLong ( 1 , id ) ; st . setInt ( 2 , offset ) ; st . setInt ( 3 , limit ) ; ResultSet rs = st . executeQuery ( ) ; while ( rs . next ( ) ) { PenaltyHistory penalty = new PenaltyHistory ( ) ; loadPenaltyHistory ( penalty , rs ) ; list . add ( penalty ) ; } } catch ( SQLException e ) { PenaltyHistoryDAOImpl . logger . error ( "listByPlayer:<seq2seq4repair_space>{}" , e ) ; } catch ( IOException e ) { PenaltyHistoryDAOImpl . logger . error ( "listByPlayer:<seq2seq4repair_space>{}" , e ) ; } finally { try { if ( conn != null ) conn . close ( ) ; } catch ( Exception e ) { } } return list ; } @ Override public void updateStatus ( List < Long > ids , Integer status ) { } @ Override public List < PenaltyHistory > listByStatus ( Integer type ) { } }

Fix:
String sqlCount = "select<seq2seq4repair_space>count(1)<seq2seq4repair_space>from<seq2seq4repair_space>(SELECT<seq2seq4repair_space>1<seq2seq4repair_space>FROM<seq2seq4repair_space>penalty_history<seq2seq4repair_space>h<seq2seq4repair_space>INNER<seq2seq4repair_space>JOIN<seq2seq4repair_space>penalty<seq2seq4repair_space>p<seq2seq4repair_space>ON<seq2seq4repair_space>h.penaltyid<seq2seq4repair_space>=<seq2seq4repair_space>p.id<seq2seq4repair_space>where<seq2seq4repair_space>p.playerid<seq2seq4repair_space>=<seq2seq4repair_space>?<seq2seq4repair_space>GROUP<seq2seq4repair_space>by<seq2seq4repair_space>h.penaltyid)<seq2seq4repair_space>c" ;