Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use of query.getSingleResult()

 
Amirr Rafique
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

 
Mihai Radulescu
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.
 
Krzysztof Koziol
Ranch Hand
Posts: 133
Eclipse IDE Java Spring
  • 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();
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic