Buggy code:
@ Repository ( "jpaRackDAO" ) class RackDAO extends DefaultDAOBase < Integer , Rack > { public RackDAO ( ) { } public RackDAO ( final EntityManager entityManager ) { } private static Criterion sameDatacenter ( final Datacenter datacenter ) { } private static Criterion equalName ( final String name ) { } public List < Rack > findRacks ( final Datacenter datacenter ) { } private static final String QUERY_GET_FILTERED_RACKS = "SELECT<seq2seq4repair_space>r.idRack,<seq2seq4repair_space>r.idDataCenter,<seq2seq4repair_space>r.name,<seq2seq4repair_space>r.shortDescription,<seq2seq4repair_space>r.largeDescription,<seq2seq4repair_space>r.vlan_id_min,<seq2seq4repair_space>r.vlan_id_max,<seq2seq4repair_space>" + ( ( ( ( ( ( "r.vlans_id_avoided,<seq2seq4repair_space>r.vlan_per_vdc_expected,<seq2seq4repair_space>r.nrsq,<seq2seq4repair_space>r.haEnabled,<seq2seq4repair_space>r.version_c<seq2seq4repair_space>FROM<seq2seq4repair_space>" + "rack<seq2seq4repair_space>r<seq2seq4repair_space>LEFT<seq2seq4repair_space>OUTER<seq2seq4repair_space>JOIN<seq2seq4repair_space>datacenter<seq2seq4repair_space>dc<seq2seq4repair_space>ON<seq2seq4repair_space>r.idDatacenter<seq2seq4repair_space>=<seq2seq4repair_space>dc.idDatacenter<seq2seq4repair_space>" ) + "WHERE<seq2seq4repair_space>dc.idDatacenter<seq2seq4repair_space>=<seq2seq4repair_space>:idDatacenter<seq2seq4repair_space>" ) + "AND<seq2seq4repair_space>(r.name<seq2seq4repair_space>like<seq2seq4repair_space>:filter<seq2seq4repair_space>" ) + "OR<seq2seq4repair_space>r.idRack<seq2seq4repair_space>in<seq2seq4repair_space>(SELECT<seq2seq4repair_space>pms.idRack<seq2seq4repair_space>FROM<seq2seq4repair_space>physicalmachine<seq2seq4repair_space>pms<seq2seq4repair_space>LEFT<seq2seq4repair_space>OUTER<seq2seq4repair_space>JOIN<seq2seq4repair_space>enterprise<seq2seq4repair_space>ent<seq2seq4repair_space>ON<seq2seq4repair_space>pms.idEnterprise<seq2seq4repair_space>=<seq2seq4repair_space>ent.idEnterprise<seq2seq4repair_space>" ) + "WHERE<seq2seq4repair_space>ent.name<seq2seq4repair_space>like<seq2seq4repair_space>:filter<seq2seq4repair_space>OR<seq2seq4repair_space>pms.name<seq2seq4repair_space>like<seq2seq4repair_space>:filter<seq2seq4repair_space>)<seq2seq4repair_space>" ) + ")" ) ; public List < Rack > findRacks ( final Datacenter datacenter , final String filter ) { } @ SuppressWarnings ( "unchecked" ) private < T > List < T > getSQLQueryResults ( final Session session , final Query query , final Class < T > objectClass , final int idFieldPosition ) { } public List < Rack > findRacksWithHAEnabled ( final Datacenter datacenter ) { } public boolean existsAnyWithDatacenterAndName ( final Datacenter datacenter , final String name ) { } public boolean existsAnyOtherWithDatacenterAndName ( final Rack rack , final String name ) { } private static final String SQL_RACK_IDS_BY_MIN_VLAN_COUNT = "SELECT<seq2seq4repair_space>rack_filtered_dc.idRack<seq2seq4repair_space>FROM<seq2seq4repair_space>" + ( ( ( "(SELECT<seq2seq4repair_space>r.idRack,<seq2seq4repair_space>r.idDatacenter,<seq2seq4repair_space>r.vlan_id_min,<seq2seq4repair_space>r.vlan_id_max,<seq2seq4repair_space>r.vlan_per_vdc_expected,<seq2seq4repair_space>r.nrsq,<seq2seq4repair_space>count(vn.id)<seq2seq4repair_space>as<seq2seq4repair_space>vlans_used<seq2seq4repair_space>" + "FROM<seq2seq4repair_space>rack<seq2seq4repair_space>r<seq2seq4repair_space>LEFT<seq2seq4repair_space>JOIN<seq2seq4repair_space>vlan_network_assignment<seq2seq4repair_space>vn<seq2seq4repair_space>ON<seq2seq4repair_space>r.idRack<seq2seq4repair_space>=<seq2seq4repair_space>vn.idRack<seq2seq4repair_space>GROUP<seq2seq4repair_space>BY<seq2seq4repair_space>r.idRack<seq2seq4repair_space>)<seq2seq4repair_space>as<seq2seq4repair_space>rack_filtered_dc<seq2seq4repair_space>" ) + "WHERE<seq2seq4repair_space>rack_filtered_dc.idDataCenter<seq2seq4repair_space>=<seq2seq4repair_space>:idDatacenter<seq2seq4repair_space>AND<seq2seq4repair_space>rack_filtered_dc.vlans_used<seq2seq4repair_space>+<seq2seq4repair_space>rack_filtered_dc.vlan_per_vdc_expected<seq2seq4repair_space>+<seq2seq4repair_space>(((rack_filtered_dc.vlan_id_max<seq2seq4repair_space>-<seq2seq4repair_space>rack_filtered_dc.vlan_id_min<seq2seq4repair_space>+1<seq2seq4repair_space>)<seq2seq4repair_space>*<seq2seq4repair_space>(rack_filtered_dc.nrsq))<seq2seq4repair_space>//<seq2seq4repair_space>100)<seq2seq4repair_space><=<seq2seq4repair_space>((rack_filtered_dc.vlan_id_max<seq2seq4repair_space>-<seq2seq4repair_space>rack_filtered_dc.vlan_id_min)<seq2seq4repair_space>+<seq2seq4repair_space>1)<seq2seq4repair_space>" ) + "ORDER<seq2seq4repair_space>BY<seq2seq4repair_space>rack_filtered_dc.vlans_used<seq2seq4repair_space>+<seq2seq4repair_space>rack_filtered_dc.vlan_per_vdc_expected<seq2seq4repair_space>ASC" ) ; private static final String SQL_RACK_IDS_BY_MIN_VLAN_COUNT_LITE = "SELECT<seq2seq4repair_space>r.idRack<seq2seq4repair_space>" + ( ( ( "FROM<seq2seq4repair_space>rack<seq2seq4repair_space>r,<seq2seq4repair_space>virtualapp<seq2seq4repair_space>va,<seq2seq4repair_space>virtualdatacenter<seq2seq4repair_space>vdc,<seq2seq4repair_space>datacenter<seq2seq4repair_space>dc<seq2seq4repair_space>" + "WHERE<seq2seq4repair_space>va.idVirtualApp<seq2seq4repair_space>=<seq2seq4repair_space>:idVApp<seq2seq4repair_space>" ) + "AND<seq2seq4repair_space>vdc.idVirtualDataCenter<seq2seq4repair_space>=<seq2seq4repair_space>va.idVirtualDataCenter<seq2seq4repair_space>" ) + "AND<seq2seq4repair_space>vdc.idDataCenter<seq2seq4repair_space>=<seq2seq4repair_space>r.idDatacenter" ) ; public List < Integer > getRackIdByMinVLANCount ( final int idDatacenter ) { } private static final String COUNT_DEPLOYED_VLA = "SELECT<seq2seq4repair_space>COUNT(vn.id)<seq2seq4repair_space>" + ( "FROM<seq2seq4repair_space>NetworkAssignment<seq2seq4repair_space>vn<seq2seq4repair_space>WHERE<seq2seq4repair_space>" + "vn.rack.id<seq2seq4repair_space>=<seq2seq4repair_space>:idRack" ) ; public Long getNumberOfDeployedVlanNetworks ( final Integer rackId ) { } public Rack findByIds ( final Integer datacenterId , final Integer rackId ) { } private static final String HQL_NOT_MANAGED_RACKS_BY_DATACENTER = ( "SELECT<seq2seq4repair_space>nmr<seq2seq4repair_space>" + ( "FROM<seq2seq4repair_space>Rack<seq2seq4repair_space>nmr<seq2seq4repair_space>WHERE<seq2seq4repair_space>" + "nmr.datacenter.id<seq2seq4repair_space>=<seq2seq4repair_space>:idDatacenter<seq2seq4repair_space>and<seq2seq4repair_space>nmr.class<seq2seq4repair_space>=<seq2seq4repair_space>" ) ) + ( Rack . class . getName ( ) ) ; public List < Rack > findAllNotManagedRacksByDatacenter ( final Integer datacenterId ) { } public List < Rack > findAllNotManagedRacksByDatacenter ( final Integer datacenterId , final String filter ) { } private final String QUERY_USED_VDRP = "SELECT<seq2seq4repair_space>vm.vdrpPort<seq2seq4repair_space>" + ( "FROM<seq2seq4repair_space>com.abiquo.server.core.cloud.VirtualMachine<seq2seq4repair_space>vm<seq2seq4repair_space>" + "WHERE<seq2seq4repair_space>vm.hypervisor.machine.rack<seq2seq4repair_space>=<seq2seq4repair_space>:rack<seq2seq4repair_space>" ) ; @ SuppressWarnings ( "unchecked" ) public List < Integer > findUsedVrdpPorts ( final Rack rack ) { } private static final String HQL_EMPTY_OFF_MACHINES_IN_RACK = "select<seq2seq4repair_space>m<seq2seq4repair_space>from<seq2seq4repair_space>Machine<seq2seq4repair_space>m<seq2seq4repair_space>where<seq2seq4repair_space>m.rack.id<seq2seq4repair_space>=<seq2seq4repair_space>:rackId<seq2seq4repair_space>and<seq2seq4repair_space>m.state<seq2seq4repair_space>=<seq2seq4repair_space>" + ( HALTED_FOR_SAVE . ordinal ( ) ) ; private static final String HQL_EMPTY_ON_MACHINES_IN_RACK = ( "select<seq2seq4repair_space>m<seq2seq4repair_space>from<seq2seq4repair_space>Machine<seq2seq4repair_space>m<seq2seq4repair_space>where<seq2seq4repair_space>m.rack.id<seq2seq4repair_space>=<seq2seq4repair_space>:rackId<seq2seq4repair_space>and<seq2seq4repair_space>m.state<seq2seq4repair_space>=<seq2seq4repair_space>" + ( MANAGED . ordinal ( ) ) ) + "<seq2seq4repair_space>and<seq2seq4repair_space>not<seq2seq4repair_space>exists<seq2seq4repair_space>(select<seq2seq4repair_space>vm<seq2seq4repair_space>from<seq2seq4repair_space>VirtualMachine<seq2seq4repair_space>vm<seq2seq4repair_space>where<seq2seq4repair_space>vm.hypervisor.machine<seq2seq4repair_space>=<seq2seq4repair_space>m)" ; public Integer getEmptyOffMachines ( final Integer rackId ) { } public Integer getEmptyOnMachines ( final Integer rackId ) { } public List < Machine > getRandomMachinesToStartFromRack ( final Integer rackId , final Integer howMany ) { } public List < Machine > getRandomMachinesToShutDownFromRack ( final Integer rackId , final Integer howMany ) { if ( howMany <= 0 ) { return null ; } Query q = getSession ( ) . createQuery ( RackDAO . HQL_EMPTY_ON_MACHINES_IN_RACK ) . setInteger ( "rackId" , rackId ) ; List < Machine > machines = q . list ( ) ; if ( machines . isEmpty ( ) ) { return null ; } <START_BUG> return machines . subList ( 0 , ( howMany < ( machines . size ( ) ) ? howMany : ( machines . size ( ) ) - 1 ) ) ; <END_BUG> } }

Fix:
return machines . subList ( 0 , ( howMany < ( machines . size ( ) ) ? howMany : machines . size ( ) ) ) ;