This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes HQL Query return type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "HQL Query return type" Watch "HQL Query return type" New topic

HQL Query return type

Ali Ekber
Ranch Hand

Joined: Jun 12, 2005
Posts: 41
I am working on an HQL hibernate query and a simplified version of the code is below:

List messages = new ArrayList();
Session session = getSession(false);
String hqlSelect = "select id, messageText, uId from Message where uId = :userId";
Query query = session.createQuery(hqlSelect);
query.setString("userId", userId);
messages = query.list();

Iterator it = messages.iterator();
while (it.hasNext()) {
System.out.println("message -> "+ ( ((Message) ((ArrayList);

I am getting ClassCast exception inside the while loop when I try to type cast the iterator to ArrayList or to Message. I think iterator is coming back as Object.

Is there any way I have the result set (List) returned as an array list of Message classes?
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

System.out.println("message -> "+ ( ((Message) ((ArrayList);

Why are you casting to ArrayList?
Each element returned by calling IS A Message.
(Message(; should work.

Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874

Hope this would work. I haven't tried it though. Even if it doesn't work. it will definitely give you a hint.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Why SQL query? Try this...

Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Why SQL query? Try this...

You mean HQL? But you make a good point, using Criteria in stead of HQL is a better way to go.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Paul Sturrock:
You mean HQL?

O'Yeah. Actually, when it starts with a select, it looks SQL to me. You know such queries - without any particular column/s to project - which starts with a from looks more HQL, IMO.
[ October 20, 2008: Message edited by: Adeel Ansari ]
It is sorta covered in the JavaRanch Style Guide.
subject: HQL Query return type
Similar Threads
Hibernate update method
Hibernate Query Language where clause
how to do this with hashtable
Tune my Spring Dao
Problem mapping pojo with view using hibernate