• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Doubt in Mikalai Zaikin study guide !!

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);

}
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
You get good luck from rubbing the belly of a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic