• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Return Type of Select (*) Count query

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic