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

Return Type of Select (*) Count query

 
Saikiran Madhavan
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I want to know, what Hibernate will return for this particular query



if we are using From query it will return that Particular Object of interest.But i am not able to figure out what it will return for the above query.

can anyone help me out in this regard

Cheers
Saikiran
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will be an Object array because it is a projection query, meaning you are stating what is in the select portion to return.

Mark
 
Sethu Nambiar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is a code snippet that explains what is returned. hope it helps

String sql = "SELECT COUNT(*) FROM table1 where col1=:col1";
Query q = s.createQuery(sql);
Integer i = (Integer)q.uniqueResult();
System.out.println("number of records in table1 is =" + i);
 
Saikiran Madhavan
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sethu,

thanks for your reply, actually your output works fine with JBoss 4.5, when we tried the same thing with WebSphere 6.1 its throwing ClassCastException for Integer.
It works fine if we typecast as Long .

why is this happening ? if you can give any clues will be helpful for us.Our development and hosting environments are different, we are developing using JBoss locally, but in our client end we are using Websphere 6.1.

cheers
sai
 
Sethu Nambiar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are you using the same database when testing with jboss and websphere. I have not tried this in different environment. But my suggestion is to use an instanceof check and cast it accordingly.

if (object instanceif Integer){
// cast it to Lnteger
}else if (object instanceof Long){
// cast it to Long
}

OR always cast it to Number because both long and integer extend it from Number. and use the appropriate method like longValue() or intValue() from the Number.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic