Java Reference
In-Depth Information
Although multiple
create
methods can be defined, only one
create
method is defined in t
he home
interface for simplicity. The
create
method takes all of the five persistent fields as argument. The
remote interface defines the
getters
for four out of the five persistent fields, except the primary key
field. The client can get the primary key field,
yachtName
, by calling the
getPrimaryKey
method of
the
EJBHome
class or
EntityContext
class.
Three
finder
methods are defined in the home interface with different data-retrieval criteria. The
findByPrimaryKey
is required and returns only one reference to
YachtEJB
that the primary key
identifies. It may return null if no match is found. The other two methods return a collection of references
to
YachtEJB
objects.
There are only
getters;
no
setters
are defined in the remote interface; therefore the
YachtEJB
is
apparently defined as read only (after created) from the clients' point of view. If the clients also need to
modify the persistent state,
setters
for the persistent fields must be defined in the remote interface.
Although
getters
and
setters
are defined in the remote interface, you do not need to code their
implementations, as you see in the
next section
.
Implementation Class with Minimum Code
In EJB 1.1, a persistent field was identified in the deployment descriptor and also identified as a public
instance variable of your bean implementation class. In EJB 2.0, this approach has been radically
changed. Persistent fields are still identified in the deployment descriptor, but they are not identified as
public-instance variables. Instead, they are identified through specialized
getters
and
setters
that
you must write.
For example, you have to write
getYachtName
,
setBuilder
, and so on in the
YachtEJB
implementation class. What is intriguing is that these methods are declared as
abstract
and are
implemented automatically by the EJB container during the deployment phase. That makes the
implementation class also abstract; thus, no instance can be instantiated directly for the implementation
class. The EJB container uses the information you provide in the deployment descriptor to automatically
generate a concrete class with all the database-access implementations. The objects of these
container-generated, concrete classes are used at runtime for clients' invocation. The implementation
class of the example
YachtEJB
is shown in
Listing 22-3
.
Listing 22-3: Implementation class of YachtEJB
/** YachtEJB Implementation Class. CMP is used.
* @author: Andrew Yang
* @version: 1.0
*/
package java_database.YachtEBean;
import java.rmi.*;
import java.util.*;
import java.sql.*;
import javax.ejb.*;
import javax.naming.*;
import common.*;
import YachtSessionSFBean.*;
public abstract class YachtBean implements EntityBean {
private EntityContext context;