Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ejbFinder Methods

 
Sunil Dixit
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.


Thanks
Sunil Dixit
 
Scott Selikoff
author
Saloon Keeper
Posts: 4010
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ådne Brunborg
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Sunil Dixit
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Sunil
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic