web server - Borland Technical Publications
web server - Borland Technical Publications
web server - Borland Technical Publications
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Container-Managed Persistence in <strong>Borland</strong> Enterprise Server<br />
There are two classes that represent the entities which correspond to these tables, the<br />
Address and Country classes. The Address class contains a direct pointer, country, to the<br />
Country entity. This direct pointer reference is an EJBObject reference; it is not a direct<br />
Java reference to the implementation bean.<br />
Now examine the code for both classes:<br />
//Address Class<br />
public class Address extends EntityBean {<br />
public int id;<br />
public String street1;<br />
public String street1;<br />
public String city;<br />
public String state;<br />
public String zip;<br />
public Country country; // this is a direct pointer to the Country<br />
}<br />
//Country Class<br />
public class Country extends EntityBean {<br />
public int id;<br />
public String name;<br />
public int exchange;<br />
public String currency;<br />
}<br />
In order for the Container to resolve the reference from the Address class to the Country<br />
class, you must specify information about the Country class in the deployment<br />
descriptor. Using the entry in the deployment descriptor, you instruct the<br />
Container to link the reference to the field Address.country to the JNDI name for the<br />
home object, CountryHome. (Look at the pigs example for a more detailed explanation.)<br />
The container optimizes this cross-entity reference; because of the optimization, using<br />
the cross reference is as fast as storing the value of the foreign key.<br />
However, there are two important differences between using a cross reference and<br />
storing the foreign key value:<br />
■<br />
■<br />
When you use a cross reference pointer to another entity, you do not have to call the<br />
other entity's home object findByPrimaryKey() method to retrieve the corresponding<br />
object entity. Using the above example as an illustration, the Address.country pointer<br />
to the Country object lets you retrieve the country object directly. You do not have to<br />
call CountryHome.findByPrimaryKey(address.country) to get the Country object that<br />
corresponds to the country id.<br />
When you use a cross reference pointer, the state of the referenced entity is only<br />
loaded when you actually use it. It is not automatically loaded when the entity<br />
containing the pointer is loaded. That is, merely loading in an Address object does<br />
not actually load in a Country object. You can think of the Address.country field as a<br />
“lazy” reference, though when the underlying object is actually used does a “lazy”<br />
reference load in its corresponding state. (Note that this “lazy” behavior is a part of<br />
the EJB model.) This facet of the EJB model results in the decoupling of the life<br />
cycle of Address.country from the life cycle of the Address bean instance itself.<br />
According to the model, Address.country is a normal entity EJBObject reference;<br />
thus, the state of Address.country is only loaded when and if it is used. The<br />
Container follows the EJB model and controls the state of AddressBean.country as it<br />
does with any other EJBObject.<br />
Container-managed field names<br />
The <strong>Borland</strong> Container has changed the container-managed persistent field names so<br />
that they are more Java friendly. SQL column names often prepend a shortened form<br />
of the table name, followed by an underscore, to each column name. For example, in<br />
the address table, there is a column for the city called addr_city. The full reference to<br />
this column is address.addr_city. With the <strong>Borland</strong> Container, this maps to the Java<br />
Chapter 14: Entity Beans and CMP 1.1 in <strong>Borland</strong> Enterprise Server 119