-->
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.  [ 6 posts ] 
Author Message
 Post subject: How do I do a Select query in Hibernate
PostPosted: Mon Apr 25, 2011 12:33 pm 
Newbie

Joined: Mon Apr 25, 2011 12:20 pm
Posts: 3
I am doing a simple login application, where the user needs to provide his username and password in order to sign in. I need to verify username and password using Hibernate.

Now with JDBC i'd
get a connection obj,
get a statement obj
get a result set obj
execute the query viz
(Select id from person where cname ='"+username+"' and passwd ='"+password+"')
if(rs.next() == true){
bla
}else{
bla
}

Now whats the equivalent in hibernate?


Top
 Profile  
 
 Post subject: Re: How do I do a Select query in Hibernate
PostPosted: Mon Apr 25, 2011 12:46 pm 
Newbie

Joined: Sun Apr 24, 2011 12:28 pm
Posts: 6
Hibernate makes use of hql.
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html


Top
 Profile  
 
 Post subject: Re: How do I do a Select query in Hibernate
PostPosted: Mon Apr 25, 2011 1:03 pm 
Newbie

Joined: Mon Apr 25, 2011 12:20 pm
Posts: 3
I know it makes use of HQL...and I have gone through the docs. but wasn't able to see the equivalence of rs.next()


Top
 Profile  
 
 Post subject: Re: How do I do a Select query in Hibernate
PostPosted: Mon Apr 25, 2011 5:13 pm 
Newbie

Joined: Sun Apr 24, 2011 12:28 pm
Posts: 6
soothsayer_hib wrote:
I know it makes use of HQL...and I have gone through the docs. but wasn't able to see the equivalence of rs.next()

I bet you where hoping for a two liner short answer on how to use Hibernate..
Read this tutorial written by Hibernate http://docs.jboss.org/hibernate/core/3. ... orial.html Or any other tutorial that you find about Hibernate using google.

Also the equivalent to rs.next() is to create a List<User> user that you fill up, and then loop through it like a normal list. For that you need to create a domain object User, a SessionFactory, mappings, config file etc etc.


Top
 Profile  
 
 Post subject: Re: How do I do a Select query in Hibernate
PostPosted: Mon Apr 25, 2011 5:15 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
There are several ways to do this depending on how you execute the query. If you just want to check if the given user exists a simple solution is:

Query query = ....
Code:
if (query.uniqueResult() != null) ....


Other options: query.list().size() > 0 or query.iterate().hasNext() or query.scroll().next()


Top
 Profile  
 
 Post subject: Re: How do I do a Select query in Hibernate
PostPosted: Mon Apr 25, 2011 5:27 pm 
Newbie

Joined: Mon Apr 25, 2011 12:20 pm
Posts: 3
Thanks Nordborg. That was precisely what I was looking for.


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