A developer is writing an application with three java Persistence API entities: order, customer, and Address. There is a many-to-one relationship between order and customer, and a one to-many relationship between customer and Address.
Which two Criteria queries will return the orders of all customers who have an address whose value specified by the String parameter postalcode? (Choose two)
A.
String postalCode = . . .
Criteria Builder cb = . . .
CriteriaQuery<order> cq = cb.createQuery (Order.class);
Root <order> order = cq.from(order.class);
Join <order, Customer> customer = order.join(Order_.customer);
Root <Order> order = cq.from (Order.class);
Join <customer, Address> address = customer join (Order_.customer)
cq.where (cb.equal (address.get(Address_.postalCode), postalCode));
cq.select (order). Distinct (true);
// query execution code here
. . .
B.
String postalCode = . . .
Criteria Builder cb = . . .
Root <Order> order = cq.from (Order.class);
order.join (order_. customer).join(Customer_.addresses);
cq.where (cb.equal (address.get(Address_.postalCode), postalCode));
cq.select (order). Distinct (true);
// query execution code here
C.
String postalCode = …
CritetiaBuilder cb = …
Root<order> order = cq – from (Order . class) ,
Join<order, Address> address = order.join(Customer_.addresses);
cq.where(ct>.equal(address.get(Address_.postalCode), postalCode));
cq-select(order).distinct(true);
// query execution code here
. . .
D.
String postalCode = …
CriteriaBuilder cb = …
Root<order> order = cq- from (Order . class ) ,
Join<order, Address> address = order . join (Order_. customer) -join (Customer_.addresses);
cq.where <cb.equal (address.get(Address_.postalCode) , postalCode) ) ;
cq.selec:(order).distinct(true);
// query execution code here
. . .
A B
A D
In Option B, address is not getting stored in local variable, and getting use in next line. This can be work, if we use order.fetch() rather than order.join().
both B and D are same here
AB
For Option D, the join Join is wrong, rather it should be Join.
Both B and D are same
what should be the the answer.
They are obviously not the same. Difference is in the next line:
B. order.join (Order_.customer).join(Customer_.addresses);
D. Join{Order, Address} address = order.join(Order_.customer).join (Customer_.addresses);
If we translate this to JPQL it is the same thing like:
o.customer.address
But, as Anish said earlier, in Option B address is not getting stored in local variable. So, B is not correct answer for sure.
I don’t know why everyone are saying that A is the correct answer, because we have duplicate local variable order that stores the Root{Order}. That’s wrong.
Following line is duplicated in A: Root{Order} order = cq.from(Order.class);
Without that line A and D would be correct answers.
both B and D are same here.
A and D are the answers which are the closest to the correct answer. I have created and tested both cases:
* first case is when you need to create two separate Join{X, Y} objects (one for customer and one for address), for example:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery{Order} cq = cb.createQuery(Order.class);
Root{Order} order = cq.from(Order.class);
Join{Order, Customer} customer = order.join(Order_.customer);
Join{Customer, Address} address = customer.join(Customer_.addresses);
ParameterExpression{String} postalCode =
cb.parameter(String.class, “postalCode”);
cq.where(cb.equal(address.get(Address_.postalCode), postalCode));
cq.select(order).distinct(true);
TypedQuery{Order} query = em.createQuery(cq);
query.setParameter(“postalCode”, “10000”);
List{Order} result = query.getResultList();
assertNotNull(result);
* the second case was when we needed to chain one Join{X, Y} object with another one:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery{Order} cq = cb.createQuery(Order.class);
Root{Order} order = cq.from(Order.class);
Join{Customer, Address} address = order.join(Order_.customer).join(Customer_.addresses);
ParameterExpression{String} postalCode =
cb.parameter(String.class, “postalCode”);
cq.where(cb.equal(address.get(Address_.postalCode), postalCode));
cq.select(order).distinct(true);
TypedQuery{Order} query = em.createQuery(cq);
query.setParameter(“postalCode”, “10000”);
List{Order} result = query.getResultList();
assertNotNull(result);