Java Reference
In-Depth Information
The business methods work just like those of the session bean. There must be a method that
exactly matches the signature for each method in the remote interface. Your accessor methods
should be a breeze.
The methods that correspond to those in the home interface have rules slightly different when
dealing with a bean written with bean-managed persistence versus container-managed persis-
tence. Because the Quote Bean you are writing is a container-managed bean I will focus on
those concepts.
Creation, as you have seen, inserts an entry into the database. There is still a matching
ejbCreate() method for each create() method in the home interface. In this method all of
the bean attributes must be initialized based on the parameters passed in. With container-
managed persistence, the database insert will be processed after the ejbCreate() method, so if
the container-managed fields are not initialized properly the insert will reflect the corrupt data.
At a minimum, the fields representing the primary key must be initialized. A bean written with
bean-managed persistence must complete the database insert inside the ejbCreate() .
For the Quote example in this chapter, you will manually supply a primary key during
creation. This is not necessarily a good technique to use. Many algorithms and design
patterns can be used to obtain a primary key for entity bean creation.
Another method is required in the creation process for entity beans. There must be an
ejbPostCreate() method with the same parameter list as each of the ejbCreate() methods.
This method is used in case any processing is required after the database insert is complete and
before the client has access to the bean.
You have now created the entity bean and have a new entry in the database. What happens
when a client wants to find this entry later? There is a finder method in the home interface
that the client will call, and because you are implementing a container-managed bean, you do
not have to write any more code. You will define the select criteria during deployment to the
application server. Writers of bean-managed beans must provide ejbFind<method> methods in
the Bean class to perform the database query. The source code for the QuoteBean is included in
Listing 12.8.
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.CreateException;
import java.rmi.RemoteException;
Search WWH ::

Custom Search