-->
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.  [ 9 posts ] 
Author Message
 Post subject: how can i get result through the query?????????
PostPosted: Mon Oct 24, 2005 8:00 am 
Newbie

Joined: Thu Sep 29, 2005 9:54 pm
Posts: 17
i have the sql query:

select count(id) from tab where statues = 2


if i execute the sql query through

hibernate find method

it's return a object

i don't konw


how can i get the count result


please tell me:(


Top
 Profile  
 
 Post subject: Re: how can i get result through the query?????????
PostPosted: Mon Oct 24, 2005 8:27 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
leng_cn wrote:
i have the sql query:

select count(id) from tab where statues = 2


if i execute the sql query through

hibernate find method

it's return a object

i don't konw


how can i get the count result


please tell me:(


Section 15.14 Tips and Tricks in the Reference doc.

Code:
Integer myCount (Integer) session.iterate("select count(id) from tab where statues = 2").next()

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject: if i use the webwork framework how can work?
PostPosted: Mon Oct 24, 2005 8:40 am 
Newbie

Joined: Thu Sep 29, 2005 9:54 pm
Posts: 17
i use the webwork framework and spring hibernate support

so i want to use the spring hibernate support method with find


the find method return a List result

well, how can i work????except the session.iterator method


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 24, 2005 8:48 am 
Regular
Regular

Joined: Mon Aug 29, 2005 9:46 am
Posts: 102
if you receive a List and wanna know the record count, do list.size();

_________________
Don't forget to rate if the post helped!


Top
 Profile  
 
 Post subject: Re: if i use the webwork framework how can work?
PostPosted: Mon Oct 24, 2005 8:50 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
leng_cn wrote:
i use the webwork framework and spring hibernate support

so i want to use the spring hibernate support method with find


the find method return a List result

well, how can i work????except the session.iterator method


session.iterate() returns a List too. That's what the .next() is for. It'll return a List that contains one object, an Integer.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject: when i use this method
PostPosted: Mon Oct 24, 2005 9:07 am 
Newbie

Joined: Thu Sep 29, 2005 9:54 pm
Posts: 17
hi pksiv
thank you for reply

i use this method get the result

String hql="Select count(id) From RegisterInfo Where status = " + sId;
Integer itmp = (Integer)registerInfoMng.findMng(hql).iterator().next();
System.out.println(itmp);


registerInfoMng has a find method

the method body is


List registerList = this.getHibernateTemplate().find(hql);
return registerList;

this is ok??

why not a output result???


Top
 Profile  
 
 Post subject: Re: when i use this method
PostPosted: Mon Oct 24, 2005 9:11 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
leng_cn wrote:
hi pksiv
thank you for reply

i use this method get the result

String hql="Select count(id) From RegisterInfo Where status = " + sId;
Integer itmp = (Integer)registerInfoMng.findMng(hql).iterator().next();
System.out.println(itmp);


registerInfoMng has a find method

the method body is


List registerList = this.getHibernateTemplate().find(hql);
return registerList;

this is ok??

why not a output result???


I don't understand your question. You get an output result. A List. I can't speak for the Spring side of things but my guess is that if you take a few seconds and look at the value in the List, you'll find that it's an Integer object.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject: Re: how can i get result through the query?????????
PostPosted: Mon Oct 24, 2005 10:42 am 
Beginner
Beginner

Joined: Thu Oct 20, 2005 1:03 pm
Posts: 38
Quote:
Code:
Integer myCount (Integer) session.iterate("select count(id) from tab where statues = 2").next()


Hi Preston,

I just used this in place of a regular query because I didn't want to go through retrieving the result set and then getting the size of it to find out the count - and I have a couple of questions about it.

First, I noticed that the current Session class doesn't have iterate methods so I had to go back to classic hibernate. Is this okay? I was under the impression that classic hibernate was old and we shouldn't be using it.

Second, is there some way to use a named query for the string we pass to the interate method? I'm trying to keep my source code free of sql.

Thanks!


Top
 Profile  
 
 Post subject: Re: how can i get result through the query?????????
PostPosted: Mon Oct 24, 2005 10:46 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
sarahsmith_books wrote:
Quote:
Code:
Integer myCount (Integer) session.iterate("select count(id) from tab where statues = 2").next()


Hi Preston,

I just used this in place of a regular query because I didn't want to go through retrieving the result set and then getting the size of it to find out the count - and I have a couple of questions about it.

First, I noticed that the current Session class doesn't have iterate methods so I had to go back to classic hibernate. Is this okay? I was under the impression that classic hibernate was old and we shouldn't be using it.

Second, is there some way to use a named query for the string we pass to the interate method? I'm trying to keep my source code free of sql.

Thanks!


I'm sure there are many ways you can do this. Just give it a try.

If you had read the comments in the deprecated iterate() method, you'd see that they suggest using find(). And I can't see why there would be any problems using a Named Query. It will still return a List with the first element being and Integer. WRITE SOME CODE, RUN IT, SEE WHAT HAPPENS.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


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