Every entity bean home interface has a mandatory findByPrimaryKey() method. The EJB specification has always allowed you to specify custom finder methods in the home interface for more specific and different lookups.

Since EJB 2.0, the specification added a standard EJB query language (EJ-BQL). Using EJBQL to replace the implementation of a finder method does not in any way affect the source of an entity bean. To add finder methods to a CMP entity bean, you need to use EJ-BQL.

For this tip, we create some customer finder in CMP EJB.

A home interface with finder:

 public interface PersonBeanHome extends EJBHome {
    
    public PersonBean create(PersonKey holding)
      throws CreateException, RemoteException;
    
    public PersonBean findByPrimaryKey(PersonKey primaryKey)
      throws FinderException, RemoteException;
    
    // our own finder method, searching persond by the first name:
    public Collection findByFirstName(String firstName)
      throws FinderException, RemoteException;
}

Deployment descriptor with EJB-QL statement:

<ejb-jar>
	<enterprise-beans>
		<!-- main bean definitions -->
		<entity>
			...
			<ejb-class>test.PersonBean</ejb-class>
			
			<!-- declaration of a primary key class  -->
			<prim-key-class>PersonKey</prim-key-class>
			<reentrant>False</reentrant>
			<cmp-version>2.x</cmp-version>
			
			<cmp-field>
				<field-name>firstName</field-name>
			</cmp-field>
			<cmp-field>
				<field-name>lastName</field-name>
			</cmp-field>
			
			<query>
				<query-method>
 				  <method-name>findByFirstName</method-name>
				  <method-params>
					<method-param>String</method-param>
				  </method-params>
				</query-method>

				<!-- a statement selecting all the persons 
                                     with the name specified: -->
				<ejb-ql>
   <![CDATA[SELECT OBJECT(p) FROM PersonBean AS p WHERE a.firstName == ?1]]>
				</ejb-ql>
			</query>
			
		</entity>
		
	</enterprise-beans>
</ejb-jar>