How do I go about designing a EJB with inheritance ?
It is easy to have it! your EJB class can extends the parent class, it is even recommended to put your model and accessor methods in the parent class and inherit it in the EJB. however for the parent methods to be accessable by the remote party you should have them in the remote interface.
Also note that the parent class method should throw the java.rmi.RemoteException and it should implements the serialize interface.
Yes. Base classes for EJB can be very handy. It works just the way that you would imagine it would... Very nice for common functionality... validation... etc.
public class MyFirstBean implements SessionBean
public class MySecondBean extends MyFirstBean
Additionally, the EJBRemote can also be extended so that you can define methods that all EJB implementations in a given project/package must support.
Depends on what sort of EJB. Inheritance where you would most like to use it - Entity Beans - is a problem. In weblogic you can do it but only if they have exactly the same primary key. You cannot even have the one primary key inheret from the other primary key.
For ejbLoad ejbStore ejbActivate etc. you can inherent
from a base EJB class. public method and member of Remote
interface can be implements also. You extends the Remote
Only ejbCreate ejbPostCreate and specifind ejbFind are to implemented on actual usually non inherent classes.
I'd like to share my experience in developing a Web application that uses inheritance in entity and Session beans.
Thibault has highlighted the options very clear. Point 2 is what is EJB component inheritance and Point 3 is class inheritance.
In EJB Component Inheritance the EJB Bean class extends from the parent Bean class,
the EJB Remote Interface extends the parent Remote Interface
The Homes have no relationship.
For Entity beans - in a hierarchy ther are other challenges involved in O-R mapping.All Entity beans in a hierarchy share the primary key class of the parent EJB. This makes sense as you never want to alter the identity of the Entity
while extending the parent.
You may use Single Table mapping for an entire hierarchy of entity beans.
use a Root-Leaf Mapping by mapping the parent(Root) to one Table and a separate Table for each sub-Bean in the Hierarchy.
Session Bean Component Hierarchies are very useful and almost indispensable in production systems - to provide the benefit of common behaviour.
My components have been deployed into IBM Webpshere and work very well.
I think your message is very clear and helpful. Do u receommend any web site which contains some examples ?
There is 3 types of inheritance with EJB:
1-Remote interface inheritance
Inheritance create a strong dependency and rigidity in the design. It must be used very carrefully, because remote interfaces need stability. Another solution is to use interface aggregation.
You must be aware about remote interface cast-up or down with narrow: the results may be very differents of cast-up or down a local class and you will probably face some strange ClassCast.
2- EJB Implementation inherite from another EJB implementation
You can face some strange behaviors such as:
- The search methods does not search for the subclasses
- The primary keys do not support inheritance
The semantic of "EJB inheritance" does not match exactly the OO semantic.
3- EJB Implementation inherite from a non distributed class
This is probably the more frequent usage of inheritance. The base class contains re-usabel code for the layer of the application.
i.e, in the Business layer containing your application business objects, you can create an AbstractBusinessObject class and extends all the EJB of this layer from this common super class.
So, I use the following rules:
- Avoid sub-classing the remote interface, use interface aggregation,
- Do not sub class an EJB from another one
- Always use implementation inheritance to share re-usable code for each layer of the architecture.
Remark, I don't have a large experience of EJB inheritance... so "handle this comments with care!".