aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Use of query.getSingleResult() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Use of query.getSingleResult()" Watch "Use of query.getSingleResult()" New topic
Author

Use of query.getSingleResult()

Amirr Rafique
Ranch Hand

Joined: Nov 14, 2005
Posts: 324

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


"Know where to find the solution and how to use it - that's the secret of success."
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

Hi

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

Object getSingleResult() returns an Object

Regards,
Mihai


SCJP, SCJD, SCWCD, OCPJBCD
Amirr Rafique
Ranch Hand

Joined: Nov 14, 2005
Posts: 324
Amirr Rafique wrote:

Double average = query.getSingleResult();



Do we need explicit casting in above case?
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

This is a downcast -> you must use it otherwise you will get a compile time error.
Krzysztof Koziol
Ranch Hand

Joined: Nov 19, 2006
Posts: 133

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();


SCJP 5.0, SCWCD 5.0, SCBCD 5.0, SCEA/OCMJEA 5.0
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Use of query.getSingleResult()