-->
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.  [ 3 posts ] 
Author Message
 Post subject: do counting records for table
PostPosted: Tue Dec 11, 2007 11:21 am 
Beginner
Beginner

Joined: Wed Oct 03, 2007 9:46 pm
Posts: 37
I use method below to do counting of total records in table. I realized there is a slow down when my records in mysql datbase grew. I only need counting no need to assign each record to my Emaillist.class object. I suspend this is not the right way to do counting. What is the best way to do counting?


Criteria crit = sess.createCriteria(EmailList.class,"emailist")
.createAlias("groupMap","groupMap")
.add( Restrictions.like("email", "%"+keyword+"%") )
.add( Restrictions.like("groupId", groupId) )
.add( Restrictions.like("companyId", companyId) ) ;


return crit.list().size();


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 11, 2007 1:06 pm 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi,

May be use an if condition to reduce the restrictions
Criteria crit = sess.createCriteria(EmailList.class,"emailist")
.createAlias("groupMap","groupMap")
if(keyword != null){
crit.add( Restrictions.like("email", "%"+keyword+"%") )
}
if(groupId!= null){
crit.add( Restrictions.like("groupId", groupId) )
}
if(companyId!= null){
crit.add( Restrictions.like("companyId", companyId) )
}

Let me check for other solution too;-)

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 11, 2007 1:27 pm 
Beginner
Beginner

Joined: Tue Apr 24, 2007 12:53 pm
Posts: 28
The best way to do a count would be to do it:


Code:
Criteria crit = sess.createCriteria(EmailList.class,"emailist")
.createAlias("groupMap","groupMap")
.add( Restrictions.like("email", "%"+keyword+"%") )
.add( Restrictions.like("groupId", groupId) )
.add( Restrictions.like("companyId", companyId) ) .setProjection(Projections.rowCount());

Integer obj = (Integer) crit.uniqueResult();





That'll get you what you're looking for.

Also, i am fairly certain you don't have to do the "%"+keyword+"%" in your clause, the Restrictions.like() functionality should take care of escaping strings for you.

-B


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