About Question enthuware.oce-jpad.v6.2.579 :
Moderators: Site Manager, fjwalraven
-
- Posts: 2
- Joined: Sat Mar 23, 2013 6:30 am
- Contact:
About Question enthuware.oce-jpad.v6.2.579 :
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é
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é
-
- Site Admin
- Posts: 10113
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.579 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Sat Mar 23, 2013 6:30 am
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.579 :
Ah, yes! I feel dumb now . Thanks for helping!
-
- Posts: 15
- Joined: Fri Apr 10, 2015 12:53 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.579 :
Hi,
I've tested this query with EclipseLink v2.5.2, and it generates the following SQL: and the result list size is 2, but adding even something as stupid as: forces it to include CustOrder's table in the SQL query: 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): yields:
I've tested this query with EclipseLink v2.5.2, and it generates the following SQL:
Code: Select all
SELECT ID FROM CUSTOMER
Code: Select all
cq.where(cb.equal(orderRoot.get("id"), orderRoot.get("id")));
Code: Select all
SELECT t1.ID FROM CUSTORDER t0, CUSTOMER t1 WHERE (t0.ID = t0.ID)
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());
so given the current wording, the first answer could also be considered correct.Customer object references: 12
Customer objects: 2
-
- Site Admin
- Posts: 10113
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.579 :
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.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):yields:Code: Select all
System.out.printf("Customer object references: %d%n", result.size()); System.out.printf("Customer objects: %d%n", new IdentityHashSet(result).size());
so given the current wording, the first answer could also be considered correct.Customer object references: 12
Customer objects: 2
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.
-
- Posts: 15
- Joined: Fri Apr 10, 2015 12:53 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.579 :
Hi,
My point was that the first two options: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.
can be interpreted either asresult List will contain 2 Customer objects.
result List will contain 12 Customer objects.
or asresult List will contain 2 references to Customer objects.
result List will contain 12 references to 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 references to 12 Customer objects.
result List will contain references to 2 Customer objects.
result List will contain 12 references to Customer objects.
-
- Site Admin
- Posts: 10113
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.579 :
OK, I see your point. Fixed.
thank you for your feedback!
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 3 guests