ejbActivate() method of a containing entity bean (LineItem) for a static relationship.

Architect's Corner
Entity bean relationships in EJB 1.1
Part 1: The basic technique

Grant Holland
Listing 3. ejbActivate() method of a containing entity bean (LineItem) for a static relationship.


public void ejbActivate() throws RemoteException
{
    // get containing entity’s primary key from EntityContext
    LineItemPK lineItemPK = entityContext.getPrimaryKey();

    // reconstitute all fields
    lineItemID = lineItemPK.lineItemID;    // get key from PK
    orderID = lineItemPK.orderID;          // get key from PK

    // use key fields to read persistent data and related key
    < read “quantity” from persistent store >
    < read related key “productName” from persistent store >
    quantity = resultSet.getString(“quantity”);
    productName = resultSet.getString(“productname”);

    // reconstitute Product EJB object for productName
    // (use Helper method)
    ProductHome home = getProductHome( “MyProduct” );
    product = home.findByPrimaryKey( new ProductPK( productName ) );
}

// Helper method to get Home object of Product entity
ProductHome getProductHome(String productHomeName ) throws NamingException
{
    Context initial = new InitialContext();
    Object objref = initial.lookup( productHomeName );
    return (ProductHome)PortableRemoteObject.narrow(
                                         objref, ProductHome.class);
}