Which two of the following statements true about Criteria query roots? (Choose two)

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



Leave a Reply 14

Your email address will not be published. Required fields are marked *


hm

hm

Really B ? Not A ? Could anyone give an example query without a root.

Bruno Mathidios

Bruno Mathidios

B,D are correct: Criteria queries may have more than one query root. This usually occurs when the query navigates from several entities.

Tiparega

Tiparega

Ops, sorry, A D

Mohamed Fayek

Mohamed Fayek

Answer : A , D

Criteria queries may have more than one query root. This usually occurs when the query navigates from several entities.

Lejmi Salah

Lejmi Salah

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.

Tommy_Croatia_ZGB

Tommy_Croatia_ZGB

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.

Tommy_Croatia_ZGB

Tommy_Croatia_ZGB

Sorry, I meant A and D are correct answers.

Lejmi Salah

Lejmi Salah

The Generics Parameter of CriteriaBuilder, CriteriaQuery, TypedQuery and List is “Employee”

Sudhir

Sudhir

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:

hmma

hmma

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.