Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Mikalai Zaikin study guide !!

 
Balaji Natarajan
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell me whether the data will get deleted in the database on calling the remove() on the collection returned in the EntityBean below.



public abstract class CustomerBean implements javax.ejb.EntityBean {
...
public void addPhoneNumber(String number, byte type)
throws NamingException, CreateException {
InitialContext jndiEnc = new InitialContext();
PhoneHomeLocal phoneHome = (PhoneHomeLocal)
(jndiEnc.lookup("PhoneHomeLocal"));
PhoneLocal phone = phoneHome.create(number,type);
Collection phoneNumbers = this.getPhoneNumbers();
phoneNumbers.add(phone);
}

public void removePhoneNumber(byte typeToRemove) {
Collection phoneNumbers = this.getPhoneNumbers();
Iterator iterator = phoneNumbers.iterator();
while(iterator.hasNext()){
PhoneLocal phone = (PhoneLocal)iterator.next();
if (phone.getType() == typeToRemove) {
phoneNumbers.remove(phone);
break;
}
}
}

public abstract java.util.Collection getPhoneNumbers( );
public abstract void setPhoneNumbers(java.util.Collection phones);

}
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure, but I would think that remove() just removes the element from the collection. It is actually the add() method which changes the DB.

In a one-to-many relationship, just invoking add() causes the phone to be first removed. So, in order to correctly add a phone, you must first invoke removePhoneNumber() followed by addPhoneNumber().
 
Dale Seng
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think HFEJB got into it too deeply, but I do think that for CMP beans, someEBObjectWithRelationship.remove(someEBObjectInRelationship) on an entity bean actually removes it from the database. Of course if you are just spinning through a collection of non-EB's, and you run remove it's not going to do anything to the database.

The examples are horrific (with A's and B's, 1's and 2's)* but this page goes into it very extensively.

--Dale--

* I had to re-write the examples with John, Paul, George and Ringo related to various instruments before I could really 'see' the relationships.
[ May 15, 2004: Message edited by: Dale Seng ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic