-->
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
PostPosted: Wed Mar 18, 2009 4:23 am 
Newbie

Joined: Tue Mar 10, 2009 10:48 pm
Posts: 18
Criteria criteria = session.createCriteria(A.class, "test")
.setProjection( Projections.projectionList()
.add( Projections.property("name").as("name_A") )
);
This is ok.


When I add in the group by as show below
Criteria criteria = session.createCriteria(A.class, "test")
.setProjection( Projections.projectionList()
.add( Projections.property("name").as("name_A") )
.add( Projections.groupProperty("test.name") )
);

I will have errors:
org.hibernate.exception.SQLGrammarException: could not execute query

How do I do the GROUP BY for createCriteria??
What is the correct name for the Property to call???

Thanks in advance!!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 18, 2009 10:16 am 
Senior
Senior

Joined: Tue Aug 01, 2006 9:24 pm
Posts: 120
I believe you want something like this

.add( Projections.property("name").as("name_A") )
.add( Projections.groupProperty("name_a") )

_________________
Please rate my replies as I need points for all of my questions also.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 18, 2009 10:16 am 
Senior
Senior

Joined: Tue Aug 01, 2006 9:24 pm
Posts: 120
I believe you want something like this

.add( Projections.property("name").as("name_A") )
.add( Projections.groupProperty("name_A") )

_________________
Please rate my replies as I need points for all of my questions also.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 19, 2009 5:21 am 
Newbie

Joined: Tue Mar 10, 2009 10:48 pm
Posts: 18
Its still giving me the same error.
org.hibernate.exception.SQLGrammarException: could not execute query


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 19, 2009 5:39 pm 
Regular
Regular

Joined: Mon Jan 05, 2009 6:42 pm
Posts: 99
Location: IL
My Database is SQL Server 2005 and here is how I have achieved.

Code:
Session session = somehow get session.
      Criteria criteria = session.createCriteria(TaskTypeFunction.class,
            "classone").setProjection(
            Projections.projectionList().add(
                  Projections.property("name").as("test")));


SQL output

Hibernate: select this_.DISPLAY_NAME as y0_ from tbl_TASK_TYPE_FUNCTION this_

List<String> objects were returned.

Now Group by Name:-

Code:
Session session = somehow get session
      Criteria criteria = session.createCriteria(TaskTypeFunction.class,
            "classone").setProjection(
            Projections.projectionList()
                  //.add(
                  //Projections.property("name").as("test")));
                  .add(
                  Projections.groupProperty("name")));


Hibernate: select this_.DISPLAY_NAME as y0_ from tbl_TASK_TYPE_FUNCTION this_ group by this_.DISPLAY_NAME

again List<String> objects are returned.

In your case you need not add the "name" in your projection. I guess If you have a group by in the HQL then Hibernate Adds it for you in the Projection also.

Hope this helps,
Latha.


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.