Java Reference
In-Depth Information
Inside your own utility
accessor
method, you can call the bean's abstract
getter
that returns a
Collection
. This tells the EJB container that you really need to get the whole list and that it is not the
container's responsibility to ensure good performance.
The
ejbPostCreate
method in
Listing 22-3
is empty. However, if there are any relationship fields, you
must put these fields' initialization code in this method. Although all the persistent fields must be set in
the
ejbCreate
method, it is important to not set any relationship fields in the
ejbCreate
methods.
When
ejbCreate
is called, the bean has not yet been inserted into the underlying database. When
calling a
setter
method, the other EJB in the relationship also tries to update its references in the
related fields. This is not possible, since the EJB that is having
ejbCreate
method invoked has not yet
been created. You should initialize the relationship fields in the
ejbPostCreate
method.
Thus, if the
YachtEJB
is related to
EngineEJB
, the
ejbPostCreate
method may look like this:
public void ejbPostCreate(String yachtName, String builder, String
engineType,
int capacity, int maxVelocity, Engine engine) {
// initialize relationship field
setEngine(null);
}
In summary, implement relationships differently for BMP entity beans and CMP entity beans. With BMP,
the code you write implements the relationships. But with CMP, the EJB container takes care of the
relationships for you. Most information of the relationships is given in the deployment descriptor. A bean
developer needs to write very little code for the simple abstract
getters
and
setters
and some
initialization in the
ejbPostCreate
method. All these features make the CMP entity bean more
appealing because they are easier to develop and more flexible.
Summary
In this chapter, you learn how CMP entity beans handle the data persistence and object relationship.
Specifically, you learned:
The differences between CMP and BMP
How to achieve persistence through persistent fields
How to handle entity relationship through relationship fields
How to specify database access in EJB query language
This chapter concludes the discussion on EJBs. Over the past three chapters, three EJB have been
developed. You are encouraged to enhance their functionality and write your own client programs to use
these EJBs. In
next chapter
, you will learn another mechanism for data persistence: the Java data
object.