-->
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.  [ 5 posts ] 
Author Message
 Post subject: Criteria Group by issue
PostPosted: Tue Apr 24, 2007 11:45 am 
Newbie

Joined: Mon May 15, 2006 10:30 am
Posts: 7
Hibernate version:
3.2.3ga

Hi,

When I execute this query, it returns a list containing Video entities:
Code:
from Video v group by v.name



When I try to reproduce the results using Criteria I only get the column "name":
Code:
Criteria crit = sess.createCriteria(Video.class);
crit.setProjection(Projections.groupProperty("name"));
crit.list();


Is there a way to have the entities returned when using Criteria?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 9:33 am 
Newbie

Joined: Thu Apr 19, 2007 1:46 am
Posts: 10
Hi ,

The point is that when you group a result with respect to a column name fetching other individual columns does not make sense rather you can get the max,min or sum or the columns grouped by the criteria specified.

For eg.

List list = criteria.setProjection(Projections.projectionList()
.add(Projections.sum("version"))
.add(Projections.groupProperty("name"))
).list();

Here I fetch the sum of version column of the rows grouped by name .
You cannot fetch the individual version values.

Even if you try to get it in oracle the error flashed would be
ORA-00979: not a GROUP BY expression


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 5:18 am 
Newbie

Joined: Mon May 15, 2006 10:30 am
Posts: 7
I know I should use distinct, but the problem still remains...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 5:33 am 
Newbie

Joined: Thu Apr 19, 2007 1:46 am
Posts: 10
Hi,

You can only use distinct with the column you have in the group by clause.

eg
select distinct(name) from table group by name

select distinct(any other column) from table group by name
is still invalid


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 5:33 am 
Regular
Regular

Joined: Mon Mar 26, 2007 12:38 am
Posts: 119
Hi,
>>> Is there a way to have the entities returned when using Criteria?
Yes.

In your case, criteria.setProjection() is used.
As per documentation, it is used to specify that the query results will be a projection (scalar in * nature).
That is why, a sclar ( and not an entity ) is returned.

-----------------------------------------------------------
Rate the reply if you find it helpful


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.