.net – Do you know of anyway to get a distinct result set without using the ResultTransformer when using SetFirstResult and SetMaxResults

.netcriteriadistinctnhibernate

When i use SetFirstResult and SetMaxResult and if the query has joins the result have duplicate results instead of unique.

Then i use all type of Distinct helpers for criteria api. But it doesnt filter the whole result set it just filters the paged result.

How can i overcome this issue ?

Thanks

Best Solution

I have found a hacky thing to overcome this issue.

The only "workaround" for this that I've been able to come up with is to issue two queries from the criteria same criteria object. The first one gets the id's the second one is contrained to the ids.

//set up crtieria as you wish, including pagination myCriteria = doStuffToSetupCriteria(); myCriteria.setFirstResult((page-1)*itemsPerPage); myCriteria.setMaxResults(itemsPerPage);

//get the list if primary keys myCriteria.setProjection(Projections.distinct(Projections.property("myAllias.id")); List ids = gacc.list();

//now add the id's into the restriction myCriteria.add(Restrictions.in("myAlias.id, ids));

//clean up from the last critiera run gacc.setProjection(null); gacc.setFirstResult(0); gacc.setMaxResults(Integer.MAX_VALUE);

//your results List objects = gacc.list()

A little hacky I agree, but the only acceptable soltion I could find given this limitiation.

Related Question