This week's book / license giveaways are in the JDBC and Relational Databases and Java in General forums.
We're giving away four copies each of PostGIS in Action and Java Advanced Topics Training and have the authors on-line!
See this thread and this one for details.
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Doubt in Mikalai Zaikin study guide !! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Doubt in Mikalai Zaikin study guide !!" Watch "Doubt in Mikalai Zaikin study guide !!" New topic
Author

Doubt in Mikalai Zaikin study guide !!

Balaji Natarajan
Greenhorn

Joined: Dec 05, 2002
Posts: 28
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

Joined: Sep 29, 2002
Posts: 1683
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().


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Dale Seng
Ranch Hand

Joined: Mar 22, 2004
Posts: 275
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Mikalai Zaikin study guide !!