Page 1 of 1

About Question enthuware.oce-jpad.v6.2.579 :

Posted: Sat Mar 23, 2013 6:34 am
by FreVdh
Isn't the answer: result List will contain 6 CustOrder objects?

Given that there are two customers in the system and each customer has 3 orders, consider following code written by a developer:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery();
Root custRoot = cq.from(Customer.class);
Root orderRoot = cq.from(CustOrder.class);
cq.select(custRoot);
TypedQuery tq = em.createQuery(cq);
List result = (List) tq.getResultList();

Which of following statements is/are correct?
The correct answer according to the test is: result List will contain 12 Customer objects.

Grtz,
Fré

Re: About Question enthuware.oce-jpad.v6.2.579 :

Posted: Sat Mar 23, 2013 6:58 am
by admin
No, since each customer has 3 orders there are 6 orders in the tables. Since it is a cartesian product (there is no join on any column), you will get 2*6 = 12 orders. This is what the explanation says as well.

HTH,
Paul.

Re: About Question enthuware.oce-jpad.v6.2.579 :

Posted: Sat Mar 23, 2013 7:36 am
by FreVdh
Ah, yes! I feel dumb now :-). Thanks for helping!

Re: About Question enthuware.oce-jpad.v6.2.579 :

Posted: Mon Apr 20, 2015 6:36 am
by Viorel Ghelbert
Hi,

I've tested this query with EclipseLink v2.5.2, and it generates the following SQL:

Code: Select all

SELECT ID FROM CUSTOMER
and the result list size is 2, but adding even something as stupid as:

Code: Select all

cq.where(cb.equal(orderRoot.get("id"), orderRoot.get("id")));
forces it to include CustOrder's table in the SQL query:

Code: Select all

SELECT t1.ID FROM CUSTORDER t0, CUSTOMER t1 WHERE (t0.ID = t0.ID)
resulting in a cartesian product between the two tables.

I'd also like to point out that even then, the result list does not technically contain 12 objects, but 12 references to 2 objects (6 references per object):

Code: Select all

System.out.printf("Customer object references: %d%n", result.size());
System.out.printf("Customer objects: %d%n", new IdentityHashSet(result).size());
yields:
Customer object references: 12
Customer objects: 2
so given the current wording, the first answer could also be considered correct.

Re: About Question enthuware.oce-jpad.v6.2.579 :

Posted: Mon Apr 20, 2015 10:12 am
by admin
Viorel Ghelbert wrote: I'd also like to point out that even then, the result list does not technically contain 12 objects, but 12 references to 2 objects (6 references per object):

Code: Select all

System.out.printf("Customer object references: %d%n", result.size());
System.out.printf("Customer objects: %d%n", new IdentityHashSet(result).size());
yields:
Customer object references: 12
Customer objects: 2
so given the current wording, the first answer could also be considered correct.
From that perspective, you cannot ever say that list contains objects because a list never contains objects. It can only contain references. So that way, even 2 is not correct.

Second thing is that it could be an implementation detail of a particular JPA implementation.

HTH,
Paul.

Re: About Question enthuware.oce-jpad.v6.2.579 :

Posted: Tue Apr 28, 2015 2:23 am
by Viorel Ghelbert
Hi,
admin wrote:From that perspective, you cannot ever say that list contains objects because a list never contains objects. It can only contain references. So that way, even 2 is not correct.
My point was that the first two options:
result List will contain 2 Customer objects.
result List will contain 12 Customer objects.
can be interpreted either as
result List will contain 2 references to Customer objects.
result List will contain 12 references to Customer objects.
or as
result List will contain references to 2 Customer objects.
result List will contain references to 12 Customer objects.
and depending on how we interpret them, both can be correct:
result List will contain references to 2 Customer objects.
result List will contain 12 references to Customer objects.

Re: About Question enthuware.oce-jpad.v6.2.579 :

Posted: Tue Apr 28, 2015 7:55 am
by admin
OK, I see your point. Fixed.
thank you for your feedback!