While working with EJB-newbies I always have to put great effort into explaining the following well known statement:
"Beans shouldn't implement their corresponding EJBObject interface."
The reasons for this are:
1. EJBObject defines five methods that the bean can't implement (this is done by the container).
2. The bean could otherwise give "this" to another bean, violating the required encapsulation of the bean in an EJBObject.
If you are missing compile time checks you can use the pattern "Business Interface". If a bean want's to pass its reference around it has to use the following statement:
But wasn't that easier in the good old times of plain RMI ?
You simple defined an interface (extending Remote and all methods throwing RemoteException) and let a class implement it !
Sure, EJBs are offering a lot more than just remote calls but they could do better by providing the following more transparent solution:
1. Remove all methods in EJBObject!
Just put them in EJBHome with the EJBObject as an parameter, e.g.:
(I know that this would stream the stub over the wire, but the container should be able to handle this.)
2. Allow passing around "this"!
If a bean passes "this" to another bean - as a method argument or return value - the container could easily wrap it up in an EJBObject.
Now we could just do it the RMI way and let the container handle the difficult things!
I request your comments