A developer wants to write a type-safe Criteria API query. Which two of the following statements true about Criteria query roots? (Choose two)
A.
The query MUST define a query root.
B.
The query MUST define a query root only if it navigates to related entities.
C.
The query MUST NOT define multiple query roots.
D.
The query may define multiple query roots.
Explanation:
http://docs.jboss.org/hibernate/orm/4.0/hem/en-US/html/querycriteria.html
http://stackoverflow.com/questions/3424696/jpa-criteria-api-how-to-add-join-clause-as-general-sentence-as-possible
B D
Really B ? Not A ? Could anyone give an example query without a root.
B,D are correct
Check this for reference:
http://docs.oracle.com/cd/E19798-01/821-1841/gjitv/index.html
B,D are correct: Criteria queries may have more than one query root. This usually occurs when the query navigates from several entities.
B,D
A B.
Criteria queries may have more than one Root, but one at least. Otherwise it would be like a SELECT without FROM.
From same explanation http://docs.jboss.org/hibernate/orm/4.0/hem/en-US/html/querycriteria.html:
A CriteriaQuery object defines a query over one or more entity, embeddable, or basic abstract schema types.
Ops, sorry, A D
Answer : A , D
Criteria queries may have more than one query root. This usually occurs when the query navigates from several entities.
B and D are correct.
The spec. is not clear about A and B.
But following code works (without root) !!!:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(Employee.class);
TypedQuery query = em.createQuery(cq);
List empList = query.getResultList();
Therefore, A is incorrect and B is correct.
The spec. is clear about D. (6.5.2 Query Roots)
6.5.2 Query Roots
…
A query may have more than one root. The addition of a query root has the semantic effect of creating a cartesian product between the entity type referenced by the added root and those of the other roots.
One guy from the stackoverflow has given a following answer to the question “Is it possible to create Criteria Query without defining a root?”
No. It is not possible. According to the JPA (2) spec, a CriteriaQuery must have at least one root.
EDIT: I was hoping to find a definitive reference in ยง6.5.2 (Query Roots) of the JPA 2 spec, but it is inconclusive on that point. However I recall a question on the OCE JPA developer exam, where exactly this question was asked and ‘one or more roots’ was the right answer. Sorry, this is the best reference I can provide.
Also interesting thing to mention is that you are not able to create JPQL query without FROM part. JPQL and Criteria sit together, providing the same possibilities. If you can’t do it in one then you can’t in the other.
So based on that I would say that B and D are correct answers.
Sorry, I meant A and D are correct answers.
The Generics Parameter of CriteriaBuilder, CriteriaQuery, TypedQuery and List is “Employee”
A ,D
Criteria queries may have more than one query root. This usually occurs when the query navigates from several entities.
The SELECT clause of the query is set by calling the select method of the query object and passing in the query root:
I agree with A, D. A root is like a table in JPQL : Select s From Student s. The root is the Student entity.
Every query needs to have at least one root.