Relationship Fields

Entity beans can form relationships with other entity beans. In Figure 6-1, at the beginning of this chapter, the Customer EJB has a one-to-one relationship with the Address EJB. The Address EJB is a fine-grained business object that should always be accessed in the context of another entity bean, which means it should have only local interfaces and not remote interfaces. An entity bean can have relationships with many different entity beans at the same time. For example, we could easily add relationship fields for Phone, CreditCard, and other entity beans to the Customer EJB. At this point, we’re choosing to keep the Customer EJB simple.

Using Figure 6-1 as a guide, we define the Address EJB as follows:

package com.titan.address; import javax.ejb.EntityContext; public abstract class AddressBean implements javax.ejb.EntityBean { public Integer ejbCreateAddress(String street, String city, String state, String zip) { setStreet(street); setCity(city); setState(state); setZip(zip); return null; } public void ejbPostCreateAddress(String street, String city, String state, String zip) { } // persistence fields public abstract Integer getId( ); public abstract void setId(Integer id); public abstract String getStreet( ); public abstract void setStreet(String street); public abstract String getCity( ); public abstract void setCity(String city); public abstract String getState( ); public abstract void setState(String state); public abstract String getZip( ); public abstract ...

Get Enterprise JavaBeans, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.