zwwcn

Just another WordPress.com site

getting distinct row count in hibernate criteria

Projections.rowCount could return a wrong result when joining is used in the criteria. I used “setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)” in the criteria to get the distinct items but it  doesn’t work with Projections.rowCount.

 

 

 
This won’t work, it still contain duplicates:

	public Criteria countDefaultCriteria(){
		return getIncidentsDefaultCriteria().setProjection(Projections.rowCount());
	}

This give the correct row count:

	public Criteria countDefaultCriteria(){
		return getIncidentsDefaultCriteria().setProjection(Projections.countDistinct("objID"));
	}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: