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.  [ 4 posts ] 
Author Message
 Post subject: Problems with createSQLQuery
PostPosted: Wed Nov 14, 2007 12:02 pm 
Newbie

Joined: Wed Dec 28, 2005 5:44 am
Posts: 12
Hibernate version:3.0

When I run the below query I get ClassCastException expecting
java.lang.Object

It is populating the list which am making sure by printing the list size which
is returning the expected value.

Lemme know where am I going wrong(please ignore the typos if any)

Code:

Integer statusId = select func_deptStatus();

Query query = getSession().creqteSQLQuery("select i.*,
func_dept(i.dept_id) as deptCount from Department i where i.dept_status_id="+statusId).
addScalar("deptCount",Hibernate.INTEGER).
addScalar("dept_id",Hibernate.INTEGER);

List<DepartmentBean> list = (List<DepartmentBean>)query.list();

System.out.println("List Size"+list.size());

return list;



The function func_dept () just gets the count of number of departments.

Code:

select count(*) from Department where status_id = <some number>



Regards,
MJ


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 14, 2007 1:13 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Your query is generating a list of Object[] containing deptCount and id. You try and cast this to a list of DepartmentBean.

Can you describe what you are trying to do?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 14, 2007 1:32 pm 
Newbie

Joined: Wed Dec 28, 2005 5:44 am
Posts: 12
Im casting it here but still doesnt solve

Code:

List<DepartmentBean> list = (List<DepartmentBean>)query.list();



Regards,
MJ


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 15, 2007 5:45 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
There's lots of information in the docs about native SQL queries. Check out
16.1. Using a SQLQuery

Basically, if you're expecting to get an entity you need to use addEntity().
e.g.
Code:
List<Cat> cats = (List<Cat>)sess.createSQLQuery("SELECT * FROM CATS").addEntity(Cat.class).list();


However, judging by your query I'm guessing DepartmentBean isn't an entity.

You could try using HQL like this:
Code:
Query query = getSession().createQuery("select new DepartmentBean(i.dept_id, func_dept(i.dept_id)) from Department i where i.dept_status_id=:deptStatusId");
List<DepartmentBean> beans = (List<DepartmentBean>)query.list();

DepartmentBean would need a constructor to match the query params:
Code:
public DepartmentBean(int deptId, int numDepts) {
...
}


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