Hi All...

As I mentioned in an earlier message, I have some working example code that 
has an EJB 2.0 CMP entity bean working with both one-to-one and one-to-many 
unidirectional relationships.

Next, I want to change the one-to-many relationship to be bidirectional. I 
expected this to be a simple change, but it didn't work out that way. I see 
that the ATM example does not include a dependent object with a 
bidirectional relationship, so I'm wondering if thie feature is known to 
work or not. If no one here has used this feature successfully, I'll add a 
bug report to bugzilla, but I wanted to make sure I did everything right 
first.

To make my changes, I added abstract set/get methods to the dependent 
object class (EmailDO.java)for the remote interface of the EB it should 
refer to (Person.java). I also modified the deployment descriptor to have a 
CMR field in the dependent relationship section and a CMP field in the 
dependent section. I realize that the presence of the <CMP-FIELD> tag 
violates the spec, but Orion will not deploy dependent objects without it 
(it complaines that it can't instansiate an abstract class that it had 
generated). It all now looks like the Order-Lineitem example in the PD1 
spec.

After Orion deployed the app, I ran it and looked at the database. Orion 
had correctly created a table to hold the dependent (since its a many it 
needs its own table), but the field for the Person EB remote interface 
(which is the EB that the dependent is created by and should refer to) was 
named VALUEPERSON and had null as its value.

This seems to indicate that Orion is not understanding that this field is 
supposed to be a CMR field. I reviewed the spec and there is no requirement 
that I manually set the Person remote interface in the dependent object, 
but I doubt thats the problem anyhow, since Orion named the person field 
"valueperson", indicating it thinks person is just another CMP field.

Anyhow, I think I did everything right and I posted the code below. If 
someone sees something wrong or has any suggestions as to what I should 
try, I would appreciate you correcting me.

Thanks VERY much!!

Jim


<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise 
JavaBeans 2.0//EN" "http://java.sun.com/j2ee/dtds/ejb-jar_2_0.dtd">
<ejb-jar>
        <description>Test Sample EJB 2.0 EB</description>
        <display-name>Person</display-name>
        <enterprise-beans>
                <entity>
                  <cmp-version>2.0</cmp-version>
                        <description>Person has an address DO</description>
                        <display-name>Test20CmpDoOM.eb.Person</display-name>
                        <ejb-name>Test20CmpDoOM.eb.PersonEJB</ejb-name>
                        <home>Test20CmpDoOM.eb.PersonHome</home>
                        <remote>Test20CmpDoOM.eb.Person</remote>
                        <ejb-class>Test20CmpDoOM.eb.PersonEJB</ejb-class>
                        <persistence-type>Container</persistence-type>
                        <prim-key-class>java.lang.String</prim-key-class>
                        <reentrant>True</reentrant>

                        <!-- These lines should not be required (according to the 
spec) -->
                        <!-- but without them deployment fails -->
                        <cmp-field><field-name>addrDo</field-name></cmp-field>
                        <cmp-field><field-name>emailDos</field-name></cmp-field>
                        
                        <cmp-field><field-name>userId</field-name></cmp-field>
                        <cmp-field><field-name>firstName</field-name></cmp-field>
                        <cmp-field><field-name>lastName</field-name></cmp-field>

                        <primkey-field>userId</primkey-field>
                </entity>
        </enterprise-beans>

  <dependents>
                <dependent>
                        <dependent-name>addrDo</dependent-name>
                        <dependent-class>Test20CmpDoOM.eb.AddrDo</dependent-class>
                        <cmp-field><field-name>street</field-name></cmp-field>
                        <cmp-field><field-name>city</field-name></cmp-field>
                        <cmp-field><field-name>state</field-name></cmp-field>
                        <cmp-field><field-name>zip</field-name></cmp-field>
                </dependent>
                <dependent>
                        <dependent-name>emailDo</dependent-name>
                        <dependent-class>Test20CmpDoOM.eb.EmailDo</dependent-class>
                        <cmp-field><field-name>email</field-name></cmp-field>
                        <cmp-field><field-name>person</field-name></cmp-field>
                </dependent>
        </dependents>

        <relationships>
                <ejb-relation>
                        <ejb-relation-name>User-Address</ejb-relation-name>
                        <ejb-relationship-role>
 
        <ejb-relationship-role-name>User-has-Address</ejb-relationship-role-name>
                                <multiplicity>one</multiplicity>
                                
<role-source><ejb-name>Test20CmpDoOM.eb.Person</ejb-name></role-source>
                                <cmr-field>
                                        <cmr-field-name>addrDo</cmr-field-name>
                                        
<cmr-field-type>Test20CmpDoOM.eb.AddrDo</cmr-field-type>
                                </cmr-field>
                        </ejb-relationship-role>
                        <ejb-relationship-role>
 
        <ejb-relationship-role-name>Address-belongsto-User</ejb-relationship-role-
name>
                                <multiplicity>one</multiplicity>
                                
<role-source><dependent-name>addrDo</dependent-name></role-source>
                        </ejb-relationship-role>
                </ejb-relation>
                
                <ejb-relation>
                        <ejb-relation-name>User-Email</ejb-relation-name>
                        <ejb-relationship-role>
                                
<ejb-relationship-role-name>User-has-Email</ejb-relationship-role-name>
                                <multiplicity>one</multiplicity>
 
        <role-source><ejb-name>Test20CmpDoOM.eb.PersonEJB</ejb-name></role-source>
                                <cmr-field>
                                        <cmr-field-name>emailDos</cmr-field-name>
                                        
<cmr-field-type>java.util.Collection</cmr-field-type>
                                </cmr-field>
                        </ejb-relationship-role>
                        <ejb-relationship-role>
 
        <ejb-relationship-role-name>Email-belongsto-User</ejb-relationship-role-na
me>
                                <multiplicity>many</multiplicity>
                                
<role-source><dependent-name>emailDo</dependent-name></role-source>
                                
                                <cmr-field>
                                        <cmr-field-name>person</cmr-field-name>
                                        <!-- 
<cmr-field-type>Test20CmpDoOM.eb.Person</cmr-field-type> -->
                                </cmr-field>
                                
                        </ejb-relationship-role>
                </ejb-relation>
        </relationships>

   <assembly-descriptor>
      <security-role>
         <description>Users</description>
         <role-name>users</role-name>
      </security-role>
   </assembly-descriptor>
</ejb-jar>

// -------------------------------------------------------------------------
// EmailDo.java
// This is a dependent object class for the Email dependent
package Test20CmpDoOM.eb;

import java.io.*;

public abstract class EmailDo {

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


        // ---------------------------------------------------------------
        // Public accessor/setters here
        // ---------------------------------------------------------------
        public abstract void setEmail(String email);
        public abstract String getEmail();
        
        // The reference to the person EJB who holds me
        public abstract void setPerson(Person person);
        public abstract Person getPerson();
}


// -------------------------------------------------------------------------
// PersonEJB.java
package Test20CmpDoOM.eb;

import java.rmi.RemoteException;
import java.util.Collection;

import java.sql.*;
import java.util.Enumeration;
import java.util.Vector;
import java.util.Date;

import javax.ejb.*;
import javax.naming.*;
import javax.sql.DataSource;
import javax.transaction.*;

public abstract class PersonEJB implements EntityBean {

    protected EntityContext ejbContext;
        
    public void setEntityContext(EntityContext ctx) {
      ejbContext = ctx;
    }

    public void unsetEntityContext() {
           ejbContext = null;
    }

    public void ejbRemove() throws RemoveException {
    }

    public void ejbLoad() {
    }

    public void ejbStore() {
    }
        
    public void ejbPostCreate() throws CreateException {
    }

    // ------------------------------------------------------------
    public String ejbCreate() {
                trace("ejbCreate() entered");
                
                initToNull();
                
                setUserId("jarcher2");
                setFirstName("Jim");
                setLastName("Archer");

                setAddress();
                
                addEmail("[EMAIL PROTECTED]");
                addEmail("[EMAIL PROTECTED]");
                
        trace("ejbCreate() exiting");

      // In CMP, should return null.
      return null;
    }

    // ------------------------------------------------------------
    public void ejbPassivate() {}
    public void ejbActivate() {}

    // ------------------------------------------------------------
        private void initToNull()
        {
                trace("initToNull() entered");

                setUserId(null);
                setFirstName(null);
                setLastName(null);

                trace("initToNull() exiting");
        }

        // ---------------------------------------------------------    
        private void trace(String text)
        {
                java.util.Date now = new java.util.Date();
                System.out.println("PersonEJB: " + now + " " + text);
        }


        // Create, get & set methods for our Address dependent object
        public abstract AddrDo createAddrDo();
        public abstract AddrDo getAddrDo();
        public abstract void setAddrDo(AddrDo addrDo);

        // Create, get & set methods for our Email dependent object
        public abstract EmailDo createEmailDo();
        public abstract Collection getEmailDos();
        public abstract void setEmailDos(Collection emailDos);

    // User Methods -------------------------------------------
    public abstract String getUserId();
    public abstract void setUserId(String id);

    public abstract String getFirstName();
    public abstract void setFirstName(String fn);
        
        public abstract String getLastName();
    public abstract void setLastName(String ln);

        public void setAddress()
        {
                AddrDo newAddr = createAddrDo();
                newAddr.setStreet("10 Crestview Drive");
                newAddr.setCity("Greenville");
                newAddr.setState("RI");
                newAddr.setZip("02828");
                
                setAddrDo(newAddr);
        }
        
         public void addEmail(String addr)
         {
                EmailDo newEmail = createEmailDo();
                
                newEmail.setEmail(addr);
                
                getEmailDos().add(newEmail);
         }
 }


// -------------------------------------------------------------------------
// Person.java
package Test20CmpDoOM.eb;

import java.rmi.RemoteException;
import javax.ejb.EJBObject;


public interface Person extends EJBObject {

        // This field will be the primary key field
    public String getUserId() throws RemoteException;
    public void setUserId(String id) throws RemoteException;

    public String getLastName() throws RemoteException;
    public void setLastName(String ln) throws RemoteException;

    public String getFirstName() throws RemoteException;
    public void setFirstName(String fn) throws RemoteException;
}

// -------------------------------------------------------------------------
// PersonHome.java
package Test20CmpDoOM.eb;

import java.rmi.RemoteException;
import java.util.*;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
import javax.ejb.FinderException;


public interface PersonHome extends EJBHome {
    Person create()
       throws CreateException, RemoteException;

    Person findByPrimaryKey(String userId)
       throws FinderException, RemoteException;
}



Reply via email to