The developer wants to write a portable criteria query that will order the orders made by customer James Brown according to increasing quantity.
Which one of the below queries correctly accomplishes that task?
A.
CriteriaBuilder cb= . . .
CriteriaQuery<order> cq = cb.createquery<order.class>
Root <customer, order> 0 = cq.from(customer.class);
Join <customer, order> 0 = c.Join(customer-.orders);
cq.where (cb.equal(c.get(customer_.name, James Brown)));
cq.orderBy (0.get (order_.quantity));
B.
CriteriaBuilder cb= . . .
CriteriaQuery<order> cq = cb.createquery<order.class>
Root <customer, order> 0 = cq.from(customer.class);
Join <customer, order> 0 = c.Join(customer-.orders);
cq.where (cb.equal(c.get(customer_.name, James Brown))); cq.select(0);
cq.orderBy (0.get (order_.quantity));
C.
CriteriaBuilder cb= . . .
CriteriaQuery<order> cq = cb.createquery<order.class>
Root <customer, order> 0 = cq.from(customer.class);
Join <customer, order> 0 = c.Join(customer-.orders);
cq.where (cb.equal(c.get(customer_.name, James Brown)));
cq.orderBy (0.get (order_.quantity));
cq.select(0);
D.
CriteriaBuilder cb= . . .
CriteriaQuery<order> cq = cb.createquery<order.class>
Root <customer, order> 0 = cq.from(customer.class);
Join <customer, order> 0 = c.Join(customer-.orders);
cq.where (cb.equal(c.get(customer_.name, James Brown)));
cq.orderBy (0.get (order_.quantity));
cq.orderBy (�quantity�);
all are wrong having malformed cq.orderBy
cb.orderBy(o.get(order_.quantity))
None of the answers are correct.
Correct answer would look like (note -> curly brackets {} are used instead of the triangular brackets):
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery{Order} cq = cb.createQuery(Order.class);
Root{Customer} c = cq.from(Customer.class);
Join{Customer, Order} o = c.join(Customer_.orders);
cq.where(cb.equal(c.get(Customer_.name), “James Brown”));
cq.orderBy(cb.asc(o.get(Order_.quantity)));
cq.select(o);
TypedQuery{Order} tq = em.createQuery(cq);
List{Order} result = tq.getResultList();
I tested it and it works.
Corresponding JPQL for that Criteria API code would look like:
select o from Customer c JOIN c.orders o where c.name = ‘James Brown’ ORDER BY o.quantity