Okay. I like a challenge. Removal of specific dependents does work. Yes I know that 
dependent
objects are going away, etc etc. This will work for the current version of orion, 
however. 

I through together a Person EJB with a userid, first and last name. I created a 
dependent object
class called phone which looks like the following:


// Phone.java
package custdo.ejb;

import java.io.*;

public abstract class Phone {

        // No longer required by the spec but required by Orion (for now)
        public abstract Phone deepCopy();


        // ---------------------------------------------------------------
        // Public accessor/setters here
        // ---------------------------------------------------------------
        public abstract void setPhoneType(String type); 
        public abstract String getPhoneType();

        public abstract void setPhoneArea(String phoneArea);
        public abstract String getPhoneArea();
        
        public abstract void setPhoneNum (String phoneNum);
        public abstract String getPhoneNum ();
        

}


In the EJB I have the usual abstract set/get/create methods.....


 public abstract Phone createPhone();
 public abstract Collection getPhones();
 public abstract void setPhones(Collection phones); 

along with these two public methods:

        
    public void addPhone(String type, String ac, String n)  
    {
                Phone phone = createPhone();
                phone.setPhoneType(type);
                phone.setPhoneArea(ac);
                phone.setPhoneNum(n);
                getPhones().add(phone);
    }
    
    public void removePhone (String type)
    {
        Collection phones = getPhones();
        Iterator iterator = phones.iterator();
        while (iterator.hasNext())
        {
                Phone phone = (Phone)iterator.next();
                System.out.println("Phone is: "+ phone);
                System.out.println("Type is: "+ phone.getPhoneType());
                System.out.println("Area is: "+ phone.getPhoneArea());
                System.out.println("Num is: " +phone.getPhoneNum());
                if (phone.getPhoneType().trim().equalsIgnoreCase(type.trim()))
                {
                        System.out.println("Type for removal is: "+type);
                        System.out.println("Removing Phone");
                        iterator.remove();
                        return;
                }
        }
    }


Okay - so then I have the following setup in the ejb-jar.xml (just the 
dependent/relationships
part)



<dependents>
        <dependent>
                <dependent-name>Phone</dependent-name>
                <dependent-class>custdo.ejb.Phone</dependent-class>
                <cmp-field><field-name>phoneType</field-name></cmp-field>
                <cmp-field><field-name>phoneArea</field-name></cmp-field>
                <cmp-field><field-name>phoneNum</field-name></cmp-field>
        </dependent>
</dependents>

<relationships>
        <ejb-relation>
                <ejb-relation-name>Person-PhoneNo</ejb-relation-name>
                <ejb-relationship-role>
                                                                  
<ejb-relationship-role-name>Person-has-Phone</ejb-relationship-role-name>
                        <multiplicity>one</multiplicity>
                        <role-source><ejb-name>Person</ejb-name></role-source>
                        <cmr-field>
                                <cmr-field-name>phones</cmr-field-name>
                                <cmr-field-type>java.util.Collection</cmr-field-type>
                        </cmr-field>
                </ejb-relationship-role>
                <ejb-relationship-role>
                                                                      
<ejb-relationship-role-name>Phone-belongsto-User</ejb-relationship-role-name>
                        <multiplicity>many</multiplicity>
                        
<role-source><dependent-name>Phone</dependent-name></role-source>
                </ejb-relationship-role>
        </ejb-relation>
</relationships>




Then I have a simple client that I just uncomment/comment as I need - nothing fancy:

import javax.rmi.*;
import javax.naming.*;
import java.util.*;

import custdo.ejb.Person;
import custdo.ejb.PersonHome;

public class PersonClient {

public static void main(String[] args) {
  try {
        
    Properties props = new Properties();
   
   
props.setProperty("java.naming.factory.initial","com.evermind.server.ApplicationClientInitialContextFactory");
                                
    props.setProperty("java.naming.provider.url","ormi://tsg-laptop1/CustDo");
    props.setProperty("java.naming.security.principal", "admin");
    props.setProperty("java.naming.security.credentials", "skarabrae");
    InitialContext initial = new InitialContext(props);
    Object objref = initial.lookup("Person");

    PersonHome home = 
      (PersonHome)PortableRemoteObject.narrow(objref, 
       PersonHome.class);

    //Person person = home.create("rharrison","Ray","Harrison");
    Person person = home.findByPrimaryKey("rharrison");
    //person.addPhone("Business","303","333-4444");
    //person.addPhone("Home","303","888-9999");
    //person.addPhone("Cell","303","999-0000");
    person.removePhone("Business");
    person.addPhone("Business","303","444-3333");

  } 
  catch (Exception ex) {
    System.err.println("Caught some unexpected exception!");
    ex.printStackTrace();
  }
} 
} 




And I get the desired results. I borrowed from various examples that have been posted, 
plus
borrowed heavily from Chapter 23 of Java Server Programming J2ee Edition, starring 
Karl Avedal, et
al. Actually the book was quite helpful in clearing up quite a bit - an excellent book 
if you all
don't have it. Granted, I have no idea whether this stuff is "to spec" and the code is 
certainly
ugly, but it does illustrate the fact that Orion does handle the removal from the 
database of the
dependent object record. 

Cheers
Ray


__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail. 
http://personal.mail.yahoo.com/

Reply via email to