-->
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: HQL-query help
PostPosted: Tue Jul 29, 2008 8:51 am 
Newbie

Joined: Wed Jul 23, 2008 2:50 am
Posts: 16
if i have follow lines in my java and using HQL,

Session session = HibernateUtil.beginTransaction();
String hql="from User where Name = 'martin' ";
Query query = session.createQuery(hql);
Object o = query.uniqueResult();



And if i have two user with name martin, how can i get/print the result?
Thanks Lars


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 8:57 am 
Regular
Regular

Joined: Wed Jan 11, 2006 12:49 pm
Posts: 64
Location: Campinas, Brazil
1) check the javadocs, you have the "list()" method, which returns the result of the query as a List.

2) Property names begin with lowercase

Regards,

Henrique

_________________
Henrique Sousa
Don't forget to rate useful responses


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 30, 2008 7:58 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Indeed, uniqueResult expects only one result to be returned, so essentially, you use this with a unique query or a query against a primary key, typically.

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=08masteringhqlandnamedqueries

For potential multiple results, you can call the list or iterate method, which will return a collection you can then loop over at your leisure.

Here's an example of a non-unique query:

Code:
Session session = HibernateUtil.beginTransaction();
String hql;
hql= "from User as u GROUP BY u.id HAVING u.id > 4)";
Query query = session.createQuery(hql);
List users = query.list();
for (int i = 0; i < users.size(); i++) {
  User user = (User) users.get(i);
  System.out.println("Hello World");
  System.out.println(user.getLoginName());
}
HibernateUtil.commitTransaction();


http://jpa.ezhibernate.com/Javacode/lea ... medqueries

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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.