Vault Tec   All about the Kinder™
I am the CEO/Co-founder of E29 Incorporated. I've been in the software industry for ten years now, have experience in about ten different computer languages, and once wrote a 60,000 line web application in three months without caffeine. I really wouldn't want to do that again.
Read more about me here, stalker.
 

Hibernate and DISTINCT calls

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?

No Comments

Leave a comment