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

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
FreVdh
Posts: 2
Joined: Sat Mar 23, 2013 6:30 am
Contact:

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

Post by FreVdh » Sat Mar 23, 2013 6:34 am

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é

admin
Site Admin
Posts: 8621
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Sat Mar 23, 2013 6:58 am

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.
If you like our products and services, please help us by posting your review here.

FreVdh
Posts: 2
Joined: Sat Mar 23, 2013 6:30 am
Contact:

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

Post by FreVdh » Sat Mar 23, 2013 7:36 am

Ah, yes! I feel dumb now :-). Thanks for helping!

Viorel Ghelbert
Posts: 15
Joined: Fri Apr 10, 2015 12:53 pm
Contact:

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

Post by Viorel Ghelbert » Mon Apr 20, 2015 6:36 am

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.

admin
Site Admin
Posts: 8621
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Mon Apr 20, 2015 10:12 am

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.
If you like our products and services, please help us by posting your review here.

Viorel Ghelbert
Posts: 15
Joined: Fri Apr 10, 2015 12:53 pm
Contact:

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

Post by Viorel Ghelbert » Tue Apr 28, 2015 2:23 am

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.

admin
Site Admin
Posts: 8621
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Tue Apr 28, 2015 7:55 am

OK, I see your point. Fixed.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests