-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Using countDistinct()
PostPosted: Fri Feb 26, 2010 5:20 am 
Newbie

Joined: Fri Feb 26, 2010 5:08 am
Posts: 2
I recently upgraded to version 3.5.0-CR-2 but it seems Projections.count() and Projections.countDistinct() return the same result, for example:

Code:
criteria.setProjection(Projections.countDistinct("id"));


will get translated into:

Code:
select count(id) from ...


where I would expect count(distinct id). Is this a bug or is there something wrong with my understanding of the countDistinct() function?


Top
 Profile  
 
 Post subject: Re: Using countDistinct()
PostPosted: Fri Feb 26, 2010 5:40 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
I suggest to debug your application by putting breakpoints to

org.hibernate.criterion.CountProjection.java methods setDistinct() and toString()


Top
 Profile  
 
 Post subject: Re: Using countDistinct()
PostPosted: Fri Feb 26, 2010 6:01 am 
Newbie

Joined: Fri Feb 26, 2010 5:08 am
Posts: 2
Thanks for the quick reply, unfortunately it doesn't look really good. It is set on distinct, but it seems to put it in the wrong place:

Code:
distinct count(id)


But I think I found what changed from 3.3.2, there used to be a specific toSqlString() that put the distinct on the correct location:

Code:
public String toSqlString(Criteria criteria, int position, CriteriaQuery criteriaQuery)
   throws HibernateException {
      StringBuffer buf = new StringBuffer();
      buf.append("count(");
      if (distinct) buf.append("distinct ");
      return buf.append( criteriaQuery.getColumn(criteria, propertyName) )
         .append(") as y")
         .append(position)
         .append('_')
         .toString();
   }


I don't know if the removal was intentional and distincts are treated somewhere else now.


Top
 Profile  
 
 Post subject: Re: Using countDistinct()
PostPosted: Fri Feb 26, 2010 6:38 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
I don't know if the removal was intentional and distincts are treated somewhere else now.


For me it looks very like a unintentional bug, because
Code:
toSqlString(Criteria criteria, int position, CriteriaQuery criteriaQuery)

is now implemented in CountProjections superclass AggregateProjection solely
where the distinct property is not handled at all.

I suggest you to implement an appropriate junit-test-case and to open an issue on Hibernate Jira.
Such issue should be threated with absolutely priority, as this bug compromises 3.5 backward compatibility.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.