File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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: 1710

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 ]
I agree. Here's the link:
subject: HQL Query return type
It's not a secret anymore!