File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes CreateException & FinderException? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "CreateException & FinderException?" Watch "CreateException & FinderException?" New topic
Author

CreateException & FinderException?

Pourang Emami
Ranch Hand

Joined: Sep 21, 2002
Posts: 127
Answers to the following question will be highly appreciated:
Is this true that within the implementation class, throwing CreateException for ejbCreate() and FinderException for ejbSelect methods is optional?
Thanks.


Best Regards,<br />Pourang Emami
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Whilst the ejbCreate() and ejbSelect() methods of an entity bean class must declare CreateException and FinderException respectively, you are not complelled to throw them. If you do wish to throw an exception from the bean code, it can only be the declared checked exception (or any of its subclasses) or any unchecked exceptions. This is standard Java.

Incidentally, and by way of contrast, a session bean's ejbCreate() method is not required to declare CreateException.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Pourang Emami
Ranch Hand

Joined: Sep 21, 2002
Posts: 127
But in "Head First EJB" page 318, an CreatExceprtion is not thrown for ejbCreate()?
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
The declaration of CreateException is optional for a BMP bean but mandatory for a CMP bean. From pages 191-192 of the EJB 2.0 spec:

The entity bean class must implement the ejbCreate<METHOD>(...) methods that correspond to the create<METHOD>(...) methods specified in the entity bean�s home interface.

The entity bean class may define zero or more ejbCreate<METHOD>(...) methods whose signatures must follow these rules:

The method name must have ejbCreate as its prefix.

The method must be declared as public.

The method must not be declared as final or static.

The return type must be the entity bean�s primary key type.

If the ejbCreate<METHOD>(...) method corresponds to a create<METHOD>(...) on the entity bean�s remote home interface, the method arguments and return value types must be legal types for RMI-IIOP.

The throws clause must define the javax.ejb.CreateException. The throws clause may define arbitrary application specific exceptions.
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Roger's right -- this is an error in the book!

You absolutely must declare that CreateException for CMP.

(I don't believe this is on the exam, though... but you'll certainly find out that it's wrong the first time you deploy a CMP bean *without* the CreateException, on a Container that verifies your bean for spec-compliance (the Reference Implementation will not allow a CMP bean to pass verification/deployment unless you have that CreateException declared).

Sorry about that folks (and once again, thanks to Roger!)
Kathy
Pourang Emami
Ranch Hand

Joined: Sep 21, 2002
Posts: 127
Thank you for your backup, but I am totally confused when I study the "Head First EJB".

A-
On page 545, The first Dumb Question says that:
"The container callbacks declared in the SessionBean and EntityBean interfaces don't declare checked exceptions."

and then at the last sentence of the answer to this auestion declares that:
"The only thing you should throw from one of the container callbacks of SessionBean, EntityBean or MessageDrivenBean is an EJBException".

But

B-
in page 548 talking about CreateException, it says that:
"If the bean's gonna throw it(CreateException), the bean's gotta declare it in the bean class, not just the home interface."

So here we have three situations!?:
1-According to our previous talks, declaring CreatException in both
bean class and Home interface is mandatory.
2-According to 'A' We should never have CreateException even in the Home interface!?
3-According to 'B' Throwing CreateException, in bean and Home interface is optional.


So CONFUSING, ....
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
You need to understand what is a container callback. This is any non-business method declared in the component interface, plus the ejbCreate() method. By a non-business method, I mean a method which the client does not see. So, a container callback includes all methods whose names begin with "ejb" plus any which set the EJBContext.

So, as an example, a stateless session bean must implement these container callbacks:

You must know that there are no checked exceptions which are declared in any of these methods. But you also know that a CMP entity bean's ejbCreate() method must declare a checked exception (CreateException). What I will say is that this ejbCreate() is called in response to a client's create() method, so it is more like a business method. I would therefore say it is appropriate to state that non-business callbacks never declare checked exceptions.

So, I reckon that the book is correct if you consider an entity bean's ejbCreate() to be a business method. I'm sure that Kathy can explain this better than I ...
Pourang Emami
Ranch Hand

Joined: Sep 21, 2002
Posts: 127
Thank you dear Roger

I hope I could get some more confirmation.
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Originally posted by Roger Chung-Wee:
So, I reckon that the book is correct if you consider an entity bean's ejbCreate() to be a business method. I'm sure that Kathy can explain this better than I ...


Actually, Roger explained it perfectly! What I said in the book *is* confusing, so it isn't your fault it doesn't make sense. As Roger said, there are container callbacks that the client does not see, and there are business methods the client does see. I was treating the create and remove and finders and home business methods as a kind of hybrid-part business method/part callback, but NOT as a *pure* container callbacks. I consider *pure* container callbacks to be ONLY those methods that the client does not see and does not directly trigger.

In other words, the home business methods, finders, create, and remove methods in the bean code DO have slightly different names than the ones the client sees in the interface, but they are still directly triggered by a client call, so we'll still count them as business methods.

Another way to tell that they are business methods and not simply container callbacks is this: pure container callbacks do NOT have transactions specified in the deployment descriptor for a CMT bean.

You can't specify a transaction on something like setSessionContext(), for example. But you DO specify a transaction on create, remove, finders, and home business methods. So that's another way to distinguish the business method callbacks (ejbCreate, ejbRemove, etc.) from the pure Container callbacks (ejbActivate, ejbPassivate, etc.).

So here are the rules:
==============================
The pure container callbacks--in other words, the methods that the client does not see and does not directly trigger, must NOT have any checked exceptions.
==============================


==============================
But all of the other business methods, including the ones whose names change inside the bean class (findByPrimaryKey() becomes ejbFindByPrimaryKey(), etc.) CAN have checked exceptions, and in the SPECIAL case of ejbCreate methods for CMP entity beans, MUST have CreateException declared in the bean code.
(This is the error in the book--we say that CreateException is optional, when in fact it is NOT optional for a CMP entity bean. For Session and BMP entity beans, CreateException IS optional.
==============================

Does that help?

If not, Roger will come to the rescue again!

cheers,
Kathy
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
As an indication of how the create, remove and finder methods are treated like business methods, see what section 18.1.1 of the EJB 2.0 spec has to say:
The javax.ejb.CreateException, javax.ejb.RemoveException, javax.ejb.FinderException, and subclasses thereof are considered to be application exceptions. These exceptions are used as standard application exceptions to report errors to the client from the create, remove,
and finder methods (see Subsections 10.5.8 and 12.1.8).
Pourang Emami
Ranch Hand

Joined: Sep 21, 2002
Posts: 127
Dear Kathy and Roger

Thank you for your answers. Now every thing is clear.
I think this is some thing to be noticed by every body who is going to take the exam.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: CreateException & FinderException?