Buggy code:
@ Path ( "coursesbytext" ) @ Produces ( MediaType . APPLICATION_JSON ) public class ServiceCourseTitleSearch { private Logger logger = Logger . getLogger ( this . getClass ( ) ) ; @ GET public ResultListCourseObject getCoursesByText ( @ QueryParam ( MetaParam . SEARCH_TEXT ) final String text , @ QueryParam ( MetaParam . RESULT_AMOUNT ) final Long count , @ QueryParam ( MetaParam . OFFSET ) final Long offset ) { IDBHandler dbHandler = ServerConfiguration . getInstance ( ) . getMiningDbHandler ( ) ; List < CourseObject > result = new ArrayList < CourseObject > ( ) ; result . add ( new CourseObject ( ) ) ; result . add ( new CourseObject ( ) ) ; if ( ( text == null ) || ( text . equals ( "" ) ) ) { return new ResultListCourseObject ( result ) ; } final Session session = dbHandler . getMiningSession ( ) ; Criteria criteria = session . createCriteria ( Course . class , "course" ) ; criteria . add ( Restrictions . ilike ( "course.title" , text , ANYWHERE ) ) ; @ SuppressWarnings ( "unchecked" ) final ArrayList < Course > courses = ( ( ArrayList < Course > ) ( criteria . list ( ) ) ) ; List < Long > ids = new ArrayList < Long > ( ) ; for ( Course course : courses ) { ids . add ( course . getId ( ) ) ; } Map < Long , Long > userMap = StudentHelper . getCourseStudentsAliasKeys ( ids , new ArrayList < Long > ( ) ) ; for ( Course courseMining : courses ) { criteria = session . createCriteria ( ILog . class , "log" ) ; criteria . add ( Restrictions . eq ( "log.course.id" , courseMining . getId ( ) ) ) ; if ( ( userMap . size ( ) ) > 0 ) { criteria . add ( Restrictions . in ( "log.user.id" , userMap . values ( ) ) ) ; } ProjectionList pl = Projections . projectionList ( ) ; pl . add ( Projections . max ( "timestamp" ) ) ; criteria . setProjection ( pl ) ; logger . info ( criteria . list ( ) . size ( ) ) ; Long lastTime = 0L ; Long firstTime = 0L ; if ( ( criteria . list ( ) . size ( ) ) > 0 ) { Long max = 0L ; for ( int i = 0 ; i < ( criteria . list ( ) . size ( ) ) ; i ++ ) { if ( ( ( criteria . list ( ) . get ( i ) ) != null ) && ( ( ( Long ) ( criteria . list ( ) . get ( i ) ) ) > max ) ) { max = ( ( Long ) ( criteria . list ( ) . get ( i ) ) ) ; break ; } } lastTime = max ; } criteria = session . createCriteria ( ILog . class , "log" ) ; criteria . add ( Restrictions . eq ( "log.course.id" , courseMining . getId ( ) ) ) ; if ( ( userMap . size ( ) ) > 0 ) { criteria . add ( Restrictions . in ( "log.user.id" , userMap . values ( ) ) ) ; } pl = Projections . projectionList ( ) ; pl . add ( Projections . min ( "timestamp" ) ) ; criteria . setProjection ( pl ) ; if ( ( criteria . list ( ) . size ( ) ) > 0 ) { Long max = 0L ; for ( int i = 0 ; i < ( criteria . list ( ) . size ( ) ) ; i ++ ) { if ( ( ( criteria . list ( ) . get ( i ) ) != null ) && ( ( ( Long ) ( criteria . list ( ) . get ( i ) ) ) > max ) ) { max = ( ( Long ) ( criteria . list ( ) . get ( i ) ) ) ; break ; } } firstTime = max ; } ServiceCourseDetails scd = new ServiceCourseDetails ( ) ; <START_BUG> final CourseObject co = new CourseObject ( courseMining . getId ( ) , courseMining . getTitle ( ) , courseMining . getTitle ( ) , userMap . size ( ) , lastTime , firstTime , scd . getCourseHash ( courseMining . getId ( ) , firstTime , lastTime ) , StudentHelper . getGenderSupport ( courseMining . getId ( ) ) ) ; <END_BUG> result . add ( co ) ; } if ( ( count != null ) && ( count > 0 ) ) { if ( ( offset != null ) && ( offset > 0 ) ) { if ( ( ( result . size ( ) ) - offset ) >= count ) { result = result . subList ( offset . intValue ( ) , ( ( offset . intValue ( ) ) + ( count . intValue ( ) ) ) ) ; } else { result = result . subList ( offset . intValue ( ) , ( ( courses . size ( ) ) - 1 ) ) ; } } else { if ( ( result . size ( ) ) > count ) { result = result . subList ( 0 , count . intValue ( ) ) ; } } } session . close ( ) ; return new ResultListCourseObject ( result ) ; } }

Fix:
final CourseObject co = new CourseObject ( courseMining . getId ( ) , courseMining . getTitle ( ) , courseMining . getTitle ( ) , StudentHelper . getStudentCount ( courseMining . getId ( ) ) , lastTime , firstTime , scd . getCourseHash ( courseMining . getId ( ) , firstTime , lastTime ) , StudentHelper . getGenderSupport ( courseMining . getId ( ) ) ) ;