File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes ejbFinder Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "ejbFinder Methods" Watch "ejbFinder Methods" New topic

ejbFinder Methods

Sunil Dixit
Ranch Hand

Joined: Sep 22, 2005
Posts: 46

Can any buddy tell me why ejbFinder method return empty collection if there is no records in database as per the condition specify in EQL.
While finder method who is returning the entity bean instance throws FinderException to indicate that there is such record in database.

Sunil Dixit
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3753

Finder methods are based on findByPrimaryKey() notion. If you have a valid key for a record, then the record must exist somewhere (since the key/record were created together).

Try to follow this logic... if you didn't use keys for finder() method, then you could return multiple records which would be a problem since the return type must be a single object, therefore, all searches must be such that *at most one record* should be returned. And with that, all searches must therefore use a key for the record. But, the record must exist before the key, so any key you have should reflect a valid record, else you could possibly return multiple records. Therefore, all finder() methods should return *exactly one record* and throw exceptions if they do not.

Finally, ejbFinder() methods are really for when you are not sure if the data you have is a key or not, therefore it may return 0, 1, or a hundred records.
[ November 29, 2005: Message edited by: Scott Selikoff ]

[OCA 8 Book] [Blog]
Ådne Brunborg
Ranch Hand

Joined: Aug 05, 2005
Posts: 208
Basically, use "single result finders" are used when there should be one and only one to match the criteria. If none match the criteria, a FinderException will be thrown - this is your basic findByPrimaryKey.

But a FinderException will also be thrown if more than one entry match the criteria. If you make a finder called findByName, and there are two entries in the database with the same name (which is possible if Name is not the primary key), a FinderException will be thrown.
[ November 29, 2005: Message edited by: �dne Brunborg ]

Entia non sunt multiplicanda praeter necessitatem
Sunil Dixit
Ranch Hand

Joined: Sep 22, 2005
Posts: 46
Hi Scott,
I m not satisfied with ur KEY explanitation..... as far as findByPrimaryKey() is concerned u r right.

Take this example of finder method in which i m not giving any KEY value in my finder method :

Student finderByName(String studentName) throws FinderException

Method take student name as argument so this method throw FinderException in following conditions :
1. When there is no record found in data base.
2. When there is more than one record found in database.

So when one finder method which is returning instance the entity bean throw finder exception to indicate that there is something goes wrong .

Then why can't one finder method which return the collection/set throw finder exception so indicate that there is something goes wrong or no record found as per the EQL.

I agree. Here's the link:
subject: ejbFinder Methods
jQuery in Action, 3rd edition