Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help in Type Casting

 
Sridhar Cheguri
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, new to the Hibernate, can anybody help in the Type casting,

Here is the complete scenario

I have a table Office_Patients, using annotations and HQL i'm retriving all the values of it when I try to get them it is giving the java.lang.ClassCastException: [Ljava.lang.Object; exception

Here is the query

Query query = getSession().createSQLQuery("select * from OFFICE_PATIENTS op " +
"where op.OFFICEID = fficeid")
.setParameter("officeid", officeID);
System.out.println("OfficePatients are " +query.list());

for(int i=0, size=query.size(); i<size; i++)
{
OfficePatients officePatients = new OfficePatients();
officePatients=(OfficePatients)query.get(i);(Exception is coming in this line

---


Thanks in Advance

Sridhar
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you are only running the query in the System.out.println statement

System.out.println("OfficePatients are " +query.list());


so later on you are not getting from the results. You are getting from the actualy Query object.

so you can try

Object[] results = query.list();

and then case results[0] if you want.

Mark
 
Sridhar Cheguri
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the reply Mark, but it is not the situation when i use in the System.out.println(), it is when I Type cast it


OfficePatients officepatients=(OfficePatients)query.list();

Here it is giving the error,

Thanks in Advance

Sridhar
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your above code you posted this

officePatients=(OfficePatients)query.get(i);(Exception is coming in this line

I see query.get(i), where is the call to list() in that line?

Mark
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sridhar Cheguri:
Thank you for the reply Mark, but it is not the situation when i use in the System.out.println(), it is when I Type cast it


OfficePatients officepatients=(OfficePatients)query.list();

Here it is giving the error,

Thanks in Advance

Sridhar


Also when you call list() it returns a Collection, not one single Object.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic