One of the guys at work asked me a question about an exam he was doing which seemed to have a very suspicious answer. If anyone can help it would be much appreciated...I dont see how " salesrep1.setCustomers(salesrep2.getCustomers()); " can possibly REMOVE any customers as the answer guide suggested (BTW: the missing image is one which showed the relationship (as described in the general comments below). The question has gotten the better of me...even the spec couldnt shed light on the matter
THE QUESTION: Consider the CMR shown in image 1. Also assume that salesrep1 contains customer1 and salesrep2 contains customer2. What will be the result when the following call is made: salesrep1.setCustomers(salesrep2.getCustomers());
Select 2 correct options a customer2 refers salesrep1.
b customer1 refers salesrep2.
c salesrep1 contains neither customer1 nor customer2.
d salesrep1 contains customer1 as well as customer2.
e None of these.
General Comments As per the given relationship and the object refereces, the situation is as follows: A SalesRep bean will have a collection of Customer beans because there is a 0..* on the Customer side of the diagram while Customer bean will have a reference to a SalesRep bean. This reference will either point to a SalesRep bean or will be null because there is a 0..1 near the SalesRep side of the diagram. Now, when you do salesrep1.setCustomers(salesrep2.getCustomers());, you are removing all customers from the Customer collection of salesrep2 and adding them to the Customer collection of salesrep1. Therefore, now, salesrep2's collection is empty and salesrep1's collection contains both customer1 as well as customer2
In the case of a one-to-many relationship, if a collection of entity objects is assigned from a cmr-field of in one instance to a cmr-field of the same relationship type in another instance, the objects in the collection are effectively moved. The contents of the collection of the target instance are replaced with the contents of the collection of the source instance, but the identity of the collection object containing the instances in the relationship does not change.
In my understanding, the target instance collection is replaced, salesrep1 contains customer2 only (not the customer 1). If it is correct, then option a is correct and what is the another option. Am I going crazy?
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
a bit of art, as a gift, that will fit in a stocking