Which one of the queries below correctly achieves this?

The developer wants to write a criteria query that will return the number of orders made by customer of each county.
Assume that customer is an entity with a unidirectional one-to-many relationship to the Order entity and that Address is an embeddable class, with an attribute country of type String.
Which one of the queries below correctly achieves this?

The developer wants to write a criteria query that will return the number of orders made by customer of each county.
Assume that customer is an entity with a unidirectional one-to-many relationship to the Order entity and that Address is an embeddable class, with an attribute country of type String.
Which one of the queries below correctly achieves this?

A.
CriteriaBuilder cb> = …
CriteriaQuery cq = cb.createQuery();
Root<Customer> c = cq.from(Customer.class);
Join<Customer, Order> o = c.join(Customer_.orders);
cq.multiselect(cb.count(0), c,get(customer_.address.get(address_.country)
cq.groupBy (c.get(customer_.address) .get(address_.country))

B.
CriteriaBuilder cb> = …
CriteriaQuery cq = cb.createQuery();
Root<Customer> c = cq.from(Customer.class); cq.select (cb.count(c.join
(customer_. Orders)) , c.get(customers(0), c.get(customer_.address) . get (Address_�country));
(c.get(Customer_.address). get(address_.country));

C.
CriteriaBuilder cb> = …
CriteriaQuery cq = cb.createQuery();
Root<Custower> c = cq.from(Customer.class);
Join<Customer, Order> o = c.join(Customer_.orders);
cq.select(cb.count(o));
cq.groupBy(c.qet(Customer__.address) – get(Address_.country)) ;

D.
CriteriaBuilder cb = …
CriteriaQuery cq = cb.createQueryO;
Root<Customer> c = cq.from(Customer.class);
Root<Customer> c = cq . from (Customer . class ) ,-Join<Customer, Order> o = c.join(Customer_.orders);
Join<Address, String> country= c.join(Customer,.address) .join(Address
cq.multiselect(cq.count(o), country );
cq.groupBy(c.get(Customer.address)- get (Address_ . country) ) ;

Explanation:
http://www.jarvana.com/jarvana/view/org/apache/openjpa/openjpa-persistence-jdbc/2.0.0/openjpa-persistence-jdbc-2.0.0-test-sources.jar!/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java?format=ok



Leave a Reply 4

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


test

test

Correct Answer Tested it 🙂

Mohamed Fayek

Mohamed Fayek

agree answer A

Tommy_Croatia_ZGB

Tommy_Croatia_ZGB

A is correct. Here is my JUnit test method that works:

/**
* JPQL: SELECT c.addressEmbeddable.country AS country, COUNT(o) FROM
* CustomerAio c JOIN c.orders o GROUP BY country
*/
@Test
private void criteriaApiTest() {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery{Tuple} cq = cb.createTupleQuery();
Root{Customer} customer = cq.from(Customer.class);
Join{Customer, Order} order = customer.join(Customer_.orders);
cq.multiselect(customer.get(Customer_.address).get(Address_.country), cb.count(order));
cq.groupBy(customer.get(Customer_.address).get(Address_.country));
TypedQuery{Tuple} query = em.createQuery(cq);
List{Tuple} result = query.getResultList();
assertNotNull(result);
}

hmma

hmma

C can be correct because it is asking for ONLY the number of orders group by each country.