File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "HQL Query return type" Watch "HQL Query return type" New topic
Author

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) it.next()).get(0)).getMessageText()));
}

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) it.next()).get(0)).getMessageText()));


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


cmbhatt
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
Bartender

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: http://aspose.com/file-tools
 
subject: HQL Query return type
 
Similar Threads
Tune my Spring Dao
how to do this with hashtable
Hibernate update method
Problem mapping pojo with view using hibernate
Hibernate Query Language where clause