22 December 2008

Getting row count in JPA using EntityManager and Query

We can make use of the following code snippet to get row count in JPA.

      Query query=em.createNativeQuery("select count(*) as rowcount from candidate where loginid=?1 or email=?2");
        query.setParameter(1,getCandidate().getLoginid().trim());
        query.setParameter(2,getCandidate().getEmail().trim());
        if( ((BigInteger)query.getSingleResult()).intValue() > 0){
            return true;
        }