Hibernate and DISTINCT calls
By Adam Kinder on Oct 3, 2008 in Programming | comments(0)
I hope I’m not pulling a Leah Culver here.. but remember, I don’t have a CS degree and Hibernate is just.. odd sometimes.
I just finished up on a very helpful requirement for our disaster application. Since we’re pre-release, I’ll be vague, but the DAO call to the database needs to select a distinct record set of cities, based on the supplied city and state name.
The code I wrote works ( beautifully by the way ), but.. is this honestly how you have to add a DISTINCT restriction to a Hibernate call?
Criteria crit = hbsession.createCriteria(-------.class);
crit.add(Restrictions.eq("state", stateName ));
crit.add(Restrictions.ilike("city", cityName + "%" ));
crit.setProjection(
Projections.distinct(
Projections.property("city")
)
);
I guess my issue is that given the rest of the Hibernate API and naming conventions, I was expecting ( and even tried ) something like this:
Criteria crit = hbsession.createCriteria(------.class);
crit.add(Restrictions.eq("state", stateName ));
crit.add(Restrictions.ilike("city", cityName + "%" ));
crit.add(Projections.distinct("city"));
Is there a better call to make when dealing with DISTINCT, or is the above code ( the working example ) the best implementation?
