• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

explanation needed

 
Edy Yu
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I saw this Collection topic posted a couple of days ago.
Still has some confusion on it.
Here is the sample code from the EJB 2.0 Spec 10.3.6.1. Can anyone explain to me why?

Collection nySalesreps = nyOffice.getSalesreps();
Collection sfSalesreps = sfOffice.getSalesreps();
Iterator i = nySalesreps.iterator();
Salesrep salesrep;
// a wrong way to transfer the salesrep
while (i.hasNext()) {
salesrep = (Salesrep)i.next();
sfSalesreps.add(salesrep); // removes salesrep from nyOffice
}
// this is a correct and safe way to transfer the salesrep
while (i.hasNext()) {
salesrep = (Salesrep)i.next();
i.remove();
sfSalesreps.add(salesrep);
}
BTW, I think the first block was wrong because it forgets to remove the sale rep from the NY Office. This simply leads to the violation of the one-to-many relationship between office and sales rep ...
[ April 22, 2004: Message edited by: Edy Yu ]
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends what the association multiplicities are. If it is 1->Many then you can simply add the item to the new relationship; the old container will automatically have the item removed from it.
If the association is Many->Many, adding the item to the new relationship will not violate anything (the new item can safely exist in both containers), and in that case, if you wanted it to exist in only one, you would have to expressly remove it from the first.
 
Edy Yu
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But why the spec says:
// a wrong way to transfer the salesrep
while (i.hasNext()) {
salesrep = (Salesrep)i.next();
sfSalesreps.add(salesrep); // removes salesrep from nyOffice
}
is wrong, if you assume the container will remove that gur for you?
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What version of the spec, and page are you referring to?
 
Edy Yu
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I stated in my initial post, it's in EJB 2.0 Spec Final Release, 10.3.6.1
 
Edy Yu
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or I guess that section was targeted to the container vendors ...
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Edy,
I read the portion of the spec that you're talking about. The point it was trying to make wasn't about CMR referential integrity (as it talked about first). The point is that you can't alter a collection while you're iterating over it.
The point in the spec was that if you have a 1->N relationship and you take an item from the collection and put it in another, it will first remove it from the original collection, then put it in the new one. If you are iterating over the first collection that would freak out (since you can't alter the collection while you're iterating over it). The point was, you should first use the iterator.remove() to remove an item from the collection (through the iterator) before adding it to the other collection (if the multiplicities are what we are assuming here).
So, it's not wrong because of what you thought. It's wrong because the underlying collection will be altered when the add is invoked, while it is being iterated over. Does that make more sense?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic