Previous Next vertical dots separating previous/next from contents/index/pdf

Query Methods and EJB QL

Workshop for WebLogic supports annotations through which you can more easily add find and select methods to your entity beans. You may already have used these annotations through the EJBGen tool provided with WebLogic Server. This topic provides an introduction to find and select methods and EJB QL. For more complete documentation on the annotations, see the EJBGen Reference.

You define find and select methods for CMP (2.0) entity beans using EJB QL. This query language, similar to SQL used in relational databases, is used to select one or more entity EJBs or entity bean fields. The WebLogic platform fully supports EJB QL 2.0 and offers a number of additional methods that can be used in conjunction with EJB QL.

Without the EJBGen annotations, you would typically specify these query methods through a combination of method declarations and the bean's deployment descriptor. Through values you specify in the annotations, Workshop for WebLogic updates the descriptor for you and generates the needed method declarations in bean interfaces.

Note. The EJB QL is used for all query methods, with the exception of findByPrimaryKey, which is automatically generated by the EJB container.

The topics in this section are:

Specifying Find Methods

A find method is invoked by other EJBs or client applications on a CMP entity bean's local or remote home interface, and returns local or remote references to one or more instances of this entity bean that match the query. A find method can return a reference to a single entity instance, such as findByPrimaryKey, or to multiple entity instances returned as a java.util.Collection. Find methods must start with the prefix find.

In Workshop for WebLogic, you specify a find method by using the @Finder annotation on the class declaration. You collect multiple find methods within an @Finders annotation. Through the @Finder annotation's attributes you indicate query method features. Basic features include the query language to use (EJB QL or WebLogic QL), which interfaces the method should be declared in (local or remote home), and the method's Java signature.

The following example illustrates two find methods declared within an ItemsBean_F entity bean. Both use EJB QL and both will appear within the bean's local home interface.

@Finders( {
        @Finder(ejbQl = "SELECT OBJECT(o) from ItemsBean_F as o " +
                "WHERE o.itemname = ?1", 
                generateOn = Finder.GenerateOn.LOCAL, 
                signature = "Collection findByItemName(java.lang.String itemname)"),
        @Finder(ejbQl = "SELECT OBJECT(i) from ManufacturerBean_F as o, " +
                "IN(o.items) AS i WHERE o.usManufacturer = 1", 
                generateOn = Finder.GenerateOn.LOCAL, 
                signature = "Collection findByUSManufacturer()")
})

Note: For more information on the @Finder annotation, see weblogic.ejbgen.Finder in the EJBGen Reference.

In the IDE, you can view and set annotation values using the Annotations view. To do this, follow these basic steps:

  1. With your bean source code visible, place your cursor in the bean class declaration.

    `

  2. In the Annotations view, scroll to where the Finders annotation is listed, right-click it, then click Add Annotation.

    Note that the @Finders annotation is written to your source code.

  3. With your cursor in the class declaration or the new @Finders annotation, in Annotations view right-click Finders, then click Add Member weblogic.ejbgen.Finder.

    The @Finder annotation is written to your source code. Note in Annotations view that all Finder attribute values are set to their default UNSPECIFIED values.

  4. In Annotations view, set Finder annotation values in the value column.

Find Method Examples

The following list shows common uses of EJB QL queries with find methods:

Select Methods

A select method is defined using EJB QL and it can either return (local or remote) references to entity beans or values of an individual CMP field. A select method is not defined in the EJB's interfaces. In other words, it is a private method that can only be used internally by a CMP entity bean class. When returning object references, a select method can return a reference to a single entity instance, or to multiple entity instances which are returned as a java.util.Collection or java.util.Set. Select methods must start with the prefix ejbSelect.

In Workshop for WebLogic, you specify a select method by adding the method's declaration to your bean class, then annotating method with the @Select annotation. As with the @Finder annotation, you use the @Select annotation's attributes to indicate query method features, such as the language to use.

The following example shows a select method declared within an ItemsBean_S entity bean. It uses EJB QL.

@Select(ejbQl = "SELECT OBJECT(o) from ItemsBean_S as o")
public abstract java.util.Collection ejbSelectAll() throws FinderException;

Note: For more information on the @Select annotation, see weblogic.ejbgen.Select in the EJBGen Reference.

In the IDE, you can view and set @Select annotation values using the Annotations view. To do this, do as described above for adding a find method, except that your cursor should be in a method declaration.

Select Method Examples

The following list shows common uses of EJB QL queries with select methods:

Standard EJB QL Operators

EJB QL 2.0 defines a number of standard operators. Some of these, like IN, DISTINCT, and the use of '.' as a navigational operator (for instance, to access a EJB's CMP field) have been described above. Other operators include:

For more detailed information on EJB QL queries and the operators defined in this language, see the Finder and Select Methods Samples, or your favorite J2EE documentation.

Related Topics

@Finder Annotation

@Select Annotation

 

Skip navigation bar   Back to Top