Accessing Other Entity Beans

   

The implementation of ejbLoad shown previously in Listing 6.7 loaded the primary key value for its associated item but it didn't load the item. This is a good approach as long as a component interface reference to the item can be obtained when it's needed. Just like any other client, the way an entity bean gets a reference to another entity is by calling a finder method on that entity's home interface. This means that the first step is to get a reference to the home interface. An EJB gets a reference to another EJB's home using an EJB reference defined in its environment. The following fragment from the auction entity's ejb-jar.xml deployment descriptor shows how you define an EJB reference to another bean's local home interface:

 <ejb-jar>    <enterprise-beans>      <entity>        <ejb-name>EnglishAuction</ejb-name>        ...        <ejb-local-ref>          <description>This EJB reference is used to locate an auction's item          </description>          <ejb-ref-name>ejb/Item</ejb-ref-name>          <ejb-ref-type>Entity</ejb-ref-type>          <local-home>com.que.ejb20.item.model.ItemHome</local-home>          <local>com.que.ejb20.item.model.Item</local>        </ejb-local-ref>        ...      </entity>      ...    </enterprise-beans>    ...  </ejb-jar> 

You place EJB references within the ejb subcontext of a bean's environment. The preceding declaration allows an auction entity to reference the local home for the item entity using the ejb-ref-name that's defined. The following deployment information shows how you then map this ejb-ref-name to the JNDI name defined for the entity if you're using WebLogic:

 <weblogic-ejb-jar>    <weblogic-enterprise-bean>      <ejb-name>EnglishAuction</ejb-name>      <reference-descriptor>        ...        <ejb-local-reference-description>          <ejb-ref-name>ejb/Item</ejb-ref-name>          <jndi-name>Item</jndi-name>        </ejb-local-reference-description>      </reference-descriptor>      ...    </weblogic-enterprise-bean>      ...  </weblogic-ejb-jar> 

With the EJB reference fully defined, an Item can be located from within EnglishAuctionBean using its primary key value. Listing 6.10 illustrates how this is done.

Listing 6.10 getItem “Retrieving a Referenced Entity Bean
 package com.que.ejb20.auction.model;  ...  public class EnglishAuctionBean extends AbstractEntity implements EntityBean {   ...    public Item getItem() {     if (getItemId() != null) {       if (item == null) {         // use lazy loading for the item reference          try {           // get a local interface reference to the item            item = getItemHome().findByPrimaryKey(itemId);          }          catch (FinderException e) {           throw new EJBException;          }        }      }      return item;    }    ...    private ItemHome getItemHome() {     InitialContext initCtx = null;      try {       // Obtain the default initial JNDI context        initCtx = new InitialContext();        // Lookup the home interface for Item that is defined as a EJB reference        // in the deployment descriptor        Object obj = initCtx.lookup( "java:comp/env/ejb/Item" );        return (ItemHome)obj;      }      catch (NamingException ex) {       throw new EJBException(ex);      }      finally {       // close the InitialContext        try {         if (initCtx != null) {           initCtx.close();          }        }        catch (Exception ex) {         throw new EJBException(ex);        }      }    }    ...  } 

When an EJB reference is looked up, the result is a reference to a home interface. You then can execute a finder method on the home and obtain a component interface reference for an associated entity bean.

The preceding example illustrated how to use an EJB reference to look up a local home interface. The process for obtaining a remote home interface is almost the same. Instead of declaring an ejb-local-ref element in the deployment descriptor, you use an ejb-ref instead. An ejb-ref identifies the remote interface and remote home for a bean using the following syntax:

 <ejb-jar>    <enterprise-beans>      <entity>        <ejb-name>EnglishAuction</ejb-name>        ...        <ejb-ref>          <description>This EJB reference is for a remote bean          </description>          <ejb-ref-name>ejb/SomeRemoteBean</ejb-ref-name>          <ejb-ref-type>Entity</ejb-ref-type>          <home>com.que.ejb20.somepackage.SomeRemoteBeanHome</home>          <remote>com.que.ejb20.somepackage.SomeRemoteBean</remote>        </ejb-ref>        ...      </entity>      ...    </enterprise-beans>    ...  </ejb-jar> 


Special Edition Using Enterprise JavaBeans 2.0
Special Edition Using Enterprise JavaBeans 2.0
ISBN: 0789725673
EAN: 2147483647
Year: 2000
Pages: 223

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net