• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Use of query.getSingleResult()

 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

EntityManager entityManager = ...;
Query query = entityManager.createQuery("SELECT AVG(EMP.AGE) FROM EMPLOYEE AS EMP");
Double average = query.getSingleResult();

As per java beat questions, about statments are right. Isn't the query.getSingleResult() return type is Object?

Please confirm

Thanks

 
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

see http://java.sun.com/javaee/5/docs/api/

Object getSingleResult() returns an Object

Regards,
Mihai

 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Amirr Rafique wrote:

Double average = query.getSingleResult();



Do we need explicit casting in above case?
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a downcast -> you must use it otherwise you will get a compile time error.
 
Ranch Hand
Posts: 133
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Amirr Rafique wrote:

Amirr Rafique wrote:

Double average = query.getSingleResult();



Do we need explicit casting in above case?



Of course we need. It should be casted in the following way:

Double average = (Double) query.getSingleResult();
 
reply
    Bookmark Topic Watch Topic
  • New Topic