sqlspringhibernatecountscrollableresults

ScrollableResults size gives repeated value


I am working on application using hibernate and spring. I am trying to get count of result got by query by using ScrollableResults but as query contains lots of join(Inner joins), the result contains id repeated many times. this creates problem for ScrollableResults when i am using it to know total no of unique rows(or unique ids) returned from database. please help. Some part of code is below :

        StringBuffer queryBuf = new StringBuffer("Some SQL query with lots of Joins");

        Query query = getSession().createSQLQuery(queryBuf.toString());

        query.setReadOnly(true);


        ScrollableResults results = query.scroll();
        if (results.isLast() == false)
            results.last();
        int total = results.getRowNumber() + 1;

        logger.debug(">>>>>>TOTAL COUNT<<<<<< = {}", total);

It gives total count 1440 but actual unique rows in database is 504. Thanks in Advance.


Solution

  • You can try

    Integer count= ((Long)query.uniqueResult()).intValue();