-->
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.  [ 2 posts ] 
Author Message
 Post subject: How can i retrieve this results
PostPosted: Fri Feb 19, 2010 5:44 pm 
Newbie

Joined: Fri Apr 10, 2009 11:44 am
Posts: 2
Hi, im new to Hibernate and im trying to get this results.

String sql = "select count(c.id), c.id from Rooms c group by c.id "

List results = getHibernateTemplate().find( sql );

if( results.size() > 0 ) {
AnObject o = list.get(0);
}

where AnObject has the members: Integer count , Long id

but it gets me a Cast Error.
Object can not be cast to AnObject.

Thanks in advance, for your help.


Top
 Profile  
 
 Post subject: Re: How can i retrieve this results
PostPosted: Sat Feb 20, 2010 2:47 pm 
Beginner
Beginner

Joined: Wed Nov 21, 2007 8:02 am
Posts: 48
in your case, result of list.get(0) is type Object[]. so you can't cast it to AnObject. you can only cast it to Object[].

But if you have a construtor like this in your AnObject:

AnObject(Integer count, Long cid)
{
this. id =cid;
this.count=count;
}

you can modify your query as below and cast it to AnObject

String sql = "select new AnObject(count(c.id), c.id ) from Rooms c group by c.id "

bytheway, what you are executing is not sql, it is hql.


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