-->
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.  [ 7 posts ] 
Author Message
 Post subject: Alternative for count(*) in Hibernate SQLQuery
PostPosted: Thu Mar 19, 2009 12:27 am 
Newbie

Joined: Fri Mar 13, 2009 4:08 am
Posts: 9
Hi,

I'm new to Hibernate. I developed a search functionality in JDBC. Now I need to convert it in hibernate. For this I'm using Hibernate SQLQuery because I need to join 3 tables.

But I'm facing a difficulty. I've a query like this :

select LOCATION, count(*) from Location_Table location left join st_sensor_table sensor on location.LOCATION = sensor.ST_SENSOR_ID where "+columnName+" like \"%"+ searchText + "%\" group by location.BM_BADGE_AT;

Now for every LOCATION I need count(*). I'm not getting the way out.
Please guide me how to do it.

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 19, 2009 4:06 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Wht do you want? HQL or SQL?

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 19, 2009 6:54 am 
Newbie

Joined: Fri Mar 13, 2009 4:08 am
Posts: 9
I'm creating SQLQuery in Hibernate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 19, 2009 7:29 am 
Beginner
Beginner

Joined: Wed Oct 03, 2007 4:10 am
Posts: 46
I post a test case in which is exemplified the use of SQLQuery

public void test(){
String sql = "Select * from table";
SQLQuery q = getSession().createSQLQuery(sql);
List l = q.list();
Iterator it = l.iterator();

while(it.hasNext()){
Object[] row = (Object[])it.next();
System.out.println("row size: " + row.length);
for(int i=0;i<row.length;i++){
System.out.println("field " + i + " :" + row[i]);
}
}

}



sql could be any VALID SQL for your database so you don't have to change anything in it if you want to port into hibernate in this way.

From my point of view your sql is not valid because you use

select field1 , count(*) from table1 group by field2


you have make group by for the fields you want to select if you use agregate functions like count()


I hope it will help you.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 19, 2009 7:33 am 
Beginner
Beginner

Joined: Wed Oct 03, 2007 4:10 am
Posts: 46
And by the way, if you use SQLQuery exhaustively in your app it does not mean that you are using hibernate, sorry to disappoint you. hibernate it is a lot more than SQLQuery.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 20, 2009 2:22 am 
Newbie

Joined: Fri Mar 13, 2009 4:08 am
Posts: 9
Thanks a lot. It solved my problem.

I'm using SQLQuery even in Hibernate because my tables were not associated earlier. Now if I'll go for association now, its going to take a long time. So I used SQLQuery.

Thanks again.


Top
 Profile  
 
 Post subject: Re: Alternative for count(*) in Hibernate SQLQuery
PostPosted: Tue Mar 08, 2011 4:53 pm 
Newbie

Joined: Tue Mar 08, 2011 4:46 pm
Posts: 1
I use a method like this to return a row count:

Code:
public static int getCount() {
   int count = 0;
   String sql = "select count(fish) from sea";
   SQLQuery query = HibernateUtil.beginTransaction().createSQLQuery(sql);
   List<Integer> listCounter = (List<Integer>)query.list();
   if (!listCounter.isEmpty()) {
      count = listCounter.get(0);
   }
   HibernateUtil.closeSession();
   return count;
}


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