Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

findXXX methods in EJB 2.x

 
Joe Harry
Ranch Hand
Posts: 10122
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

I have a method that has the following signature,

public Collection findByUserId() throws FinderException, now what will my Collection be if there are no users found? Will it throw a FinderException or will it not? If it is not throwing a FinderException, what value will my collection have? Will it be null or an empty Collection?
 
Kalyan Anand
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe it should be empty collection
 
Steffen Neustadt
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jothi Shankar Kumar Sankararaj:
Guys,

I have a method that has the following signature,

public Collection findByUserId() throws FinderException, now what will my Collection be if there are no users found? Will it throw a FinderException or will it not?

IMHO
if a function shall trigger an exception it has to build an exception-oject and initiate the handling of the exception. therefore use the key-word "throw"


If it is not throwing a FinderException, what value will my collection have? Will it be null or an empty Collection?

you define it in the find-method by initialisating the return value...
..as I would say...
 
Joe Harry
Ranch Hand
Posts: 10122
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steffen Neustadt:

you define it in the find-method by initialisating the return value...
..as I would say...


I really did not get you...
 
Steffen Neustadt
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jothi Shankar Kumar Sankararaj:


I really did not get you...


in my opinion you habe a method findxy() with return value of typ collection, right?
So you have to return a value of this typ, f.i.
public collection findxy(){
//what ever you do, somewhere you have to get a value c
// c=new Collection(); c.add(xy);...
//c=what you have found;
return c;

}

so if nothing found c=empty, or you gave it an other value in your method...
 
Joe Harry
Ranch Hand
Posts: 10122
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me put my question in this way,

I have in my HomeInterface the following declaration,



And in my caller, I have the following definition,



Now in my caller above, what if my Collection is empty? Will it throw a FinderException?
 
Scott Selikoff
author
Saloon Keeper
Posts: 4014
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These are great questions but why not try it out? Since you have the code all ready it should be easy enough to setup and try yourself.
 
Joe Harry
Ranch Hand
Posts: 10122
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just verified this and when we return a Collection or an Enumeration, we do not get a FinderException.
 
Joe Harry
Ranch Hand
Posts: 10122
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When it is not throwing a FinderException, then why do I have to explicitly say throws FinderException in my component interfaces?
 
raj
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
"findbyprimarkey" is a finder method,if this method returns a collection of keys(against a single key) then we will get "finderexception"
that's why we should throw finderexceptions for finder methods.
for other finder methods, we will return collection.if no records are found to return, then the method will return a empty collection object(not null).
 
raj
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we are returning a collection for finder methods,
can we use list or set as a return type for finder methods??
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"banti_mca2004", please check your private messages.
[ October 06, 2008: Message edited by: Paul Sturrock ]
 
Joe Harry
Ranch Hand
Posts: 10122
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rajeshmanepalli rajeshmanepalli:
hi,
"findbyprimarkey" is a finder method,if this method returns a collection of keys(against a single key) then we will get "finderexception"
that's why we should throw finderexceptions for finder methods.
for other finder methods, we will return collection.if no records are found to return, then the method will return a empty collection object(not null).


Your post seems contradictory to what I have tested. Please be clear and format your sentences. I find it hard to follow.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic