EJB 2.0 Changes to Bean-Managed Persistence
The changes and improvements to bean-managed persistence are not nearly as extensive as those for
container-managed persistence. There are basically two:
-
A developer of an EJB entity using bean-managed persistence can take advantage of fine-
grained entities using local interfaces, rather than dependent value objects, in implementing
the object model. This will solve state data-aliasing problems, but not relationship data-
aliasing problems.
-
A developer can use the new home interface business methods to implement business methods
for operations that are not specific to a particular entity; for example, methods that apply to
groups of entities or that do not need any entity instances to exist.
Rules for Using Home Interface Business Methods
Home business methods are the EJB equivalent of class (static) methods in general Java development. In
other words, they are an appropriate place for functionality that applies to that class of object, rather
than a particular object.
For each home business method declared in the home interface, there must be a corresponding method
in the implementation class with "ejbHome" prepended to the method name. Because there is no entity
identity associated with the instance when it is executing a home business method, the bean developer
must not access functionality associated with a particular entity. This includes the accessor methods and
the entity context.
A BMP Example Using Home Business Methods
Let's look at an example of how an entity bean with bean-managed persistence can take advantage of the
new home interface methods. We'll use a simple entity that represents measurements in the database and
uses bean-managed persistence. Once it has been recorded in the database, this bean is read-only and our
only requirements are for reporting. We'll access the data we need in two ways: first, using a finder method
and iterating through instances on the client; and second, by using a home business method.
This example comprises five files:
| File Name | File Description |
| Measurement.java | The entity remote component interface. |
| MeasurementHome.java | The entity remote home interface. |
| MeasurementEJB.java | The entity implementation class. |
| ejb-jar.xml | The deployment descriptor. |
| Client.java | A simple file to test the application. |
Remote Component Interface
A remote interface would often replace these fine-grained accessors with a view object. We have defined
our component to be read-only once created; note that there are no setter accessors in this interface.
package measurements;
import java.util.Date;
import java.rmi.RemoteException;
import javax.ejb.EJBObject;
public interface Measurement extends EJBObject {
public long getId() throws RemoteException;
public Date getEventTimestamp() throws RemoteException;
public String getEventName() throws RemoteException;
public double getEventMagnitude() throws RemoteException;
}
Remote Home Interface
Along with the typical finder and create methods, this interface defines a business method. This is new
for EJB 2.0:
package measurements;
import java.util.Date;
import java.util.Collection;
import java.rmi.RemoteException;
import javax.ejb.EJBHome;
import javax.ejb.FinderException;
import javax.ejb.CreateException;
public interface MeasurementHome extends EJBHome {
public Measurement create(long measurementId, Date timestamp,
String eventName,
double magnitude) throws CreateException,
RemoteException;
public Measurement findByPrimaryKey(Long measurementId)
throws FinderException, RemoteException;
The totalMagnitudeForRange() business method is declared here. Note that it doesn't make sense
for this method to be in the component interface, because it may apply to more than one measurement:
public double totalMagnitudeForRange(Date from,
Date to) throws RemoteException;
public Collection findInDate(Date from, Date to)
throws RemoteException, FinderException;
}
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.