public Map < Long , List < QueryQuestionComment > > listTreesByQueryPage ( QueryPage queryPage ) { Map < Long , List < QueryQuestionComment > > result = new HashMap <> ( ) ; EntityManager entityManager = getEntityManager ( ) ; CriteriaBuilder criteriaBuilder = entityManager . getCriteriaBuilder ( ) ; CriteriaQuery < QueryQuestionComment > criteria = criteriaBuilder . createQuery ( QueryQuestionComment . class ) ; Root < QueryQuestionComment > root = criteria . from ( QueryQuestionComment . class ) ; criteria . select ( root ) ; criteria . where ( criteriaBuilder . and ( criteriaBuilder . equal ( root . get ( QueryQuestionComment_ . queryPage ) , queryPage ) , criteriaBuilder . equal ( root . get ( QueryQuestionComment_ . archived ) , Boolean . FALSE ) , criteriaBuilder . isNotNull ( root . get ( QueryQuestionComment_ . parentComment ) ) ) ) ; List < QueryQuestionComment > allChildren = entityManager . createQuery ( criteria ) . getResultList ( ) ; for ( QueryQuestionComment comment : allChildren ) { Long parentCommentId = comment . getParentComment ( ) . getId ( ) ; List < QueryQuestionComment > children = result . get ( parentCommentId ) ; if ( children == null ) { children = new ArrayList <> ( ) ; result . put ( parentCommentId , children ) ; } children . add ( comment ) ; } return result ; }
Lists all non - root comments on page and orders them to map with parentComment . id as key and list of comments directly below parentComment as value .