Thursday, July 12, 2007

Different of ejbSelect and ejbFind

ejbSelect or find?

i found that the ejbSelectxxx and findxxx method are very similar,the only difference is that it seem like the ejbSelectxxx is not exposed to the client directly,you must wrapper it in a business method,then call the method from client,but i think i can use the findxxx method instead of the ejbSelectxxx method,who can tell me how can i decide when or where i use which method?

reply
First there is no relation between ejbSelect() and ejbFind(). You don't implement ejbFind() methods in CMP entity beans' classes, so you can't call an ejbSelect() method from an ejbFind() method.
Second, in CMP you don't write any persistence logic in your bean class. The container do this. But suppose you want to have a (home) business method. How will look the implementation of this method? No JDBC cals...
Here is the place of ejbSelect() methods. You tell the container how to implement this method just like with ejbFind() methods - with EJB QL. Then from your business method you call the ejbSelect() method. The most powerful thing here is that the result of this invocation can be anything - incl. persistent fields, not only Primary Keys.
Of course if you have relations with other entity beans the result will be an EJBObject ( or a collection ).

Finder and select methods use EJB QL queries to return objects and state information of entity beans using container-managed persistence.

A select method is similar to a finder method in the following ways:

1. A select method can return a local or remote interface (or a collection of interfaces).
2. A select method queries a database.
3. The deployment descriptor specifies an EJB QL query for a select method.
4. The entity bean class does not implement the select method.

However, a select method differs significantly from a finder method:

1. A select method can return a persistent field (or a collection thereof) of a related entity bean. A finder method can return only a local or remote interface (or a collection of interfaces).
2. Because it is not exposed in any of the local or remote interfaces, a select method cannot be invoked by a client. It can be invoked only by the methods implemented within the entity bean class. A select method is usually invoked by either a business or a home method.
3. A select method is defined in the entity bean class. For bean-managed persistence, a finder method is defined in the entity bean class, but for container-managed persistence it is not.


Code

public abstract class AddressBean implements javax.ejb.EntityBean {
...

// Customer home methods.
// These are wrappers of ejbSelect methods

public Collection ejbHomeQueryZipCodes(String state)
throws FinderException {
return ejbSelectZipCodes(state);
}

public Collection ejbHomeQueryAll()
throws FinderException {
return ejbSelectAll();
}

public CustomerLocal ejbHomeQueryCustomer(AddressLocal addr)
throws FinderException {
return ejbSelectCustomer(addr);
}
...
}




ref1
ref2
ref3

No comments: