Chapter 8. EJB 2.0 CMP: EJB QL

Find methods have been a part of the Enterprise JavaBeans specification since EJB 1.0. These methods are defined on the entity bean’s local and remote home interfaces and are used for locating entity beans. All entity beans must have a findByPrimaryKey() method, which takes the primary key of the entity bean as an argument and returns a reference to an entity bean. For example, the Cruise EJB defines the standard primary key find method in its home interface as follows:

public CruiseHomeLocal extends javax.ejb.EJBLocalHome 
{
    public Integer create(String name,ShipLocal ship);
    
    public CruiseLocal findByPrimaryKey(Integer key);

}

In addition to the mandatory findByPrimaryKey() method, entity bean developers may define as many custom find methods as they like. For example, the Cruise EJB might define a method, such as findByName(), for locating a Cruise with a specific name:

public CruiseHomeLocal extends javax.ejb.EJBLocalHome 
{
    public Integer create(String name,ShipLocal ship)
        throws CreateException;
    
    public CruiseLocal findByPrimaryKey(Integer key)
        throws FinderException;

    public CruiseLocal findByName(String cruiseName)
        throws FinderException;
}

The option of defining custom find methods is nothing new, but until EJB 2.0 there was no standard way of defining how the find methods should work. The behavior of the findByPrimaryKey() method is obvious: find the entity bean with the same primary key. However, the behavior of custom find methods is not always ...

Get Enterprise JavaBeans, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.