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.
7) Which statement is correct about the EntityManager API?
a) The merge, persist, remove, and getReference methods must be invoked within a transaction context.
b) It is safe (no exception is thrown) to call merge or getTransaction on a JTA EntityManager instance.
c) The getReference method can throw an EntityNotFoundException. (*)
d) Runtime exceptions thrown by the refresh and createQuery methods of the EntityManager interface do NOT cause the transaction to be rolled back.
REFERENCE:
Option A is incorrect. getReference does NOT need to be invoked within a transaction context. (See JPA Specification 3.1.1.)
Option B is incorrect. An exception is thrown if you call getTransaction on a JTA transaction manager. (See the JPA Specification 3.1.1 interface listing.)
Option C is correct. Even though in general this behaves as a lazy proxy to an entity, an EntityNotFoundException can be thrown. (See the JPA Specification 3.1.1 interface documentation.)
Option D is incorrect. It causes the exception to roll back. (See JPA Specification 3.1.1.)
public <T> T getReference(Class<T> entityClass, Object primaryKey);
/**
* Synchronize the persistence context to the
* underlying database.
* @throws TransactionRequiredException if there is
* no transaction
* @throws PersistenceException if the flush fails
*/