• 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
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Doubt in Sun's sample question

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question below is from the following link:webpage


9) Given an excerpt from an entity:
10. @Entity
11. public class Koala {
12. int children;
13. @Id
14. private Integer id;
15. /* ... */
16. }

The following code was written to find the sum and average number of children across all Koala entities (assume the variable em is bound to a valid EntityManager instance):
20. String query = "SELECT SUM(k.children), AVG(k.children) from Koala k";
21. Query q = em.createQuery(query);
22. Object[] res = (Object []) q.getSingleResult();

Which two statements are correct? (Choose two.)
a) There is a syntax error in the JPQL on line 20.
b) If the variable o is NOT null, the types of the two values in the array are Integer and Double respectively. (*)
c) There is an error in the typecasting on line 22, which results in a runtime or compile-time exception.
d) If the variable o is NOT null, the types of the two values in the array are Double and Double respectively.
e) The syntax of the JPQL statement on line 20 is valid and the code executes without an error. (*)
f) A runtime exception is generated on line 22.

REFERENCE:
Option E and Option B are correct. This is a valid JPQL query. (See 4.8.4 of the JPA Specification.) The Type of the return value is (Object[]) (see 3.6.1 of the JPA Specification) and in this case, the MAX aggregate function returns Integer and AVG a Double (See 4.8.4 of the JPA Specification.)
Option A, C, D and F are incorrect because there is no syntax error in the query or the code.




How can option e and b be correct???
Isn't SUM supposed to return Long when applied to state-fields of integral types???
Also, in line 22, how can getSingleResult() return an array of Object??? So option e also should be incorrect.
Please help............
Thanks in advance!
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Minu Jain wrote:

How can option e and b be correct???
Isn't SUM supposed to return Long when applied to state-fields of integral types???
Also, in line 22, how can getSingleResult() return an array of Object??? So option e also should be incorrect.
Please help............
Thanks in advance!



SUM aggregate function can return the following types: Long, Double, BigInteger and BigDecimal depending on the
field type.

Which means, Theoretically (i've never tried) that since you can Narrow a Long into an Integer, the result could be
an int as well. How this works under the hood, i have no idea, but since the field "children" is marked as "int" i would
expect to get an "int" as a result of the SUM aggregate function...

For the second question, is interesting, ...

I will try that, but maybe is a way to avoid being thrown the javax.persistence.NonUniqueResultException when
the result is not unique... even though there will be only one Row returned from the query, there are actually two
columns in a real database query...one with the SUM and one with the result of the AVG...which are not Koala
entities and i'm not sure how they are seen from the entity manager...

Dave
 
Davide Crudo
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...Actually now I've found the rule:

Any Query with SELECT cause, if the select, like in your case, contains multiple select expressions like:



The rule says that the returned result type is of type Object[].

So, in the specific case, the single query result, has one singe result which is of type Object[] and not of type Object.
This is why, you get an Array as a single result and not multiple results in an Array!

Dave
 
Minu Jain
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much.....
 
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic