wood burning stoves 2.0*
The moose likes EJB and other Java EE Technologies and the fly likes ejbSelect Method in CMP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "ejbSelect Method in CMP" Watch "ejbSelect Method in CMP" New topic
Author

ejbSelect Method in CMP

Arundhathi Menon
Ranch Hand

Joined: Jan 14, 2004
Posts: 113
Hi all,
Pls cld u tell me how to call the ejbSelect Method in the CMP entity bean..cld u give a sample code if possible,
Thanks a ton,
regards,
Menon


SCJP , SCWCD , SCBCD , SCDJWS
Appu Chan
Greenhorn

Joined: Aug 29, 2002
Posts: 28
Hi,
See an extract below from the EJB book by Monson Haefel.
ejbSelect methods are similar to finder methods, but they are more versatile and can be used only internally, by the bean class. In other words, select methods are private query methods; they are not exposed to an entity bean's clients through the home interfaces.

Another difference between the select and find methods is the transaction context under which they execute. The select method executes in the transaction context of the business or callback method that is using it, while the find methods execute according to their own transaction attributes, as specified by the bean provider.
Select methods are declared as abstract methods using the naming convention ejbSelect<METHOD-NAME>. The following code shows four select methods declared in the AddressBean class:
public class AddressBean implements javax.ejb.EntityBean {
...
public abstract String ejbSelectMostPopularCity()
throws FinderException;

public abstract Set ejbSelectZipCodes(String state)
throws FinderException;
public abstract Collection ejbSelectAll()
throws FinderException;
public abstract CustomerLocal ejbSelectCustomer(AddressLocal addr)
throws FinderException;
...
}
Select methods can return the values of CMP fields. The ejbSelectMostPopularCity() method, for example, returns a single String value, the name of the city referenced by the most Address EJBs.
To return several references from a select method, you must declare the return type to be either a java.util.Collection or java.util.Set. A select method that uses a Set return type will not have duplicate values, while a Collection return type may have duplicates. Multi-entity selects return an empty Collection or Set if no matching beans are found. The ejbSelectZipCodes() method returns a java.util.Set of String values: a unique collection of all the Zip Codes declared for the Address EJBs for a specific state.
The arguments are used as input parameters in the EJB QL statements assigned to the select methods.
Select methods can return local or remote EJB objects. For single-entity select methods, the type is determined by the return type of the ejbSelect() method. Multi-entity select methods, which return a collection of EJB objects, return local EJB objects by default.
The following snippet from an XML deployment descriptor declares two of the select methods from the above example. Notice that they are exactly the same as the find method declarations. Find and select methods are declared in the same part of the deployment descriptor, within a <query> element inside an <entity> element:
<query>
<query-method>
<method-name>ejbSelectZipCodes</method-name>
<method-params>
<method-param>java.lang.String</method-param>
</method-params>
</query-method>
<ejb-ql>
SELECT a.homeAddress.zip FROM Address AS a
WHERE a.homeAddress.state = ?1
</ejb-ql>
</query>
<query>
<query-method>
<method-name>ejbSelectAll</method-name>
<method-params/>
</query-method>
<result-type-mapping>Remote</result-type-mapping>
<ejb-ql>
SELECT OBJECT(a) FROM Address AS a
</ejb-ql>
</query>
The name given in each <method-name> element must match one of the ejbSelect<METHOD-NAME>() methods defined in the bean class. This is different from find methods in CMP, which do not have a corresponding ejbFind method in the bean class. For find methods, we use the method name in the local or remote home interface. Select methods, on the other hand, use the names of select methods defined by the bean class.

The value of <result-type-mapping> can be either Remote or Local. A value of Local indicates that the select method should return local EJB objects; Remote indicates remote EJB objects. If the <result-type-mapping> element is not declared, the default is Local. For single-entity select, the actual return type of the ejbSelect() method must match the <result-type-mapping>. For example, if a single-entity ejbSelect() method returns an EJBObject type, the <result-type-mapping> must be Remote. In the previous example, the <result-type-mapping> element for the ejbSelectAll() method is declared as Remote, which means the query should return remote EJB object types (i.e., remote references to the Address EJB).
Select methods are not limited to the context of any specific entity bean. They can be used to query across all the entity beans declared in the same deployment descriptor. Select methods may be used by the bean class from its ejbHome() methods, from any business methods, or from the ejbLoad() and ejbStore() methods. In most cases, select methods will be called from ejbHome() or from business methods in the bean class.
The most important thing to remember about select methods is that while they can do anything find methods can and more, they can be used only by the entity bean class that declares them, not by the entity bean's clients.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ejbSelect Method in CMP
 
Similar Threads
ejbSelect...() method
About select methods in CMP entity bean class
Auto Generate Primary Key
update records with CMP entity bean
EJB QL: Difference between find and select methods