Search code examples
javaoraclehibernatehibernate-criteria

What is the difference between Projections.distinct(Projections.count("objectId")) and Projections.countDistinct("objectId")


Projections.distinct(Projections.count("objectId")) 

and

Projections.countDistinct("objectId")

Could any one tell me what is the difference between them? When to use one over other? Which is the proper way for getting distinct count? Is the generated query same for both statements?


Solution

  • Projections.distinct(Projections.count("objectId")) 
    

    Projections.distinct could use combination of projections e.g.

    Projections.distinct(projectionsList)
    

    where projectionsList is defined like this

    ProjectionList projectionsList  = Projections.projectionList();
    projectionsList .add(Projections.property("id"));
    projectionsList .add(Projections.property("name"));
    ...
    

    Projections.countDistinct() is just one field based projection.

    So in fact no difference. The first could be used if you need distinct count of some fields combination