The EJB deployment descriptor
editor can help you define inheritance relationships between enterprise
beans.
You can use the tools to define two forms of inheritance:
- Standard class inheritance
- EJB inheritance
In standard class inheritance, the home interface, remote interface,
or enterprise bean class inherits properties and methods from base
classes that are not themselves enterprise bean classes or interfaces.
You would specify the bean superclass using the superclass field of
the enterprise bean wizard.
In EJB inheritance, by comparison, an enterprise bean inherits
properties (such as CMP fields and relationship roles), methods, and
method-level assembly descriptor attributes from another enterprise
bean that resides in the same EJB module. To assist you in defining
an EJB inheritance relationship, the New Enterprise Bean wizard allows
you to create an enterprise bean that inherits from an existing enterprise
bean. The EJB editor allows you to change the inheritance structure
of existing enterprise beans, which updates the generated Java™ code.
This product supports several other ways to change the parent using
the wizard available from the Inheritance section of the EJB editor:
- Move a bean class in an inheritance structure (not the root) to
a different inheritance structure (change the parent).
- Add a parent to an existing bean class that is not currently in
an inheritance structure. The inherited bean, and all its subtypes,
now references the key class of its supertype. The key shape of the
inherited bean changes, and as a result, methods of its bean class
and remote information can change or be removed. If the old key class
is no longer referenced by another Java class,
it can be safely deleted.
- Remove a bean from an inheritance structure, so that it becomes
a root. You must specify a key class. The EJB tools create a key for
you if you chose to do so. It has the same attributes as the bean's
previous key, so the class/bean is still valid. For example, suppose
that you have Employee as the parent of PTEmployee. When you move
PTEmployee out, PTEmployeeKey can be created as a new key class. When
you click OK, the tools preserve, reflect,
and automatically generate the same fields.
Some of the characteristics of EJB inheritance are:
- For CMP entity beans, there is support for single table and root-leaf
table mapping. However, large hierarchies (wide or deep) might perform
poorly because of the large size of the SQL statements. Root-leaf
mapping is more susceptible to this issue because of the number of
joins involved in the SQL statement.
- The deployment descriptor lists all the CMP fields for each enterprise
bean, including those fields that have been inherited. Method permissions
and method transactions that are defined for a supertype EJB are created
for each subtype enterprise bean. This is also true for EJB references.
- When an inheritance relationship is defined between enterprise
beans, all home methods that are not part of an association are copied
to the child bean. The child home interface does not extend the home
interface of the supertype enterprise bean in order to have specific
create methods on subtypes, without having to support the supertype
create methods.
- The bean class of the new child enterprise bean extends the bean
class of the parent enterprise bean. No parent bean class methods
are defined in the child bean class.
- The local and remote interfaces extend the remote interface of
the supertype enterprise bean.
- Key classes are common to all inherited enterprise beans. In other
words, the key class in the child enterprise bean is identical to
the key class in the parent enterprise bean.
- You can only add to the key at the root bean.
Source page and EJB inheritance
If
you are modifying CMP entity beans in an inheritance hierarchy, you should
use the wizards and interface portions of the EJB deployment descriptor
editor, not the Source page. For example, if you want to add or remove
CMP fields or change the primary key field of a CMP bean, the tools
synchronize these fields for all inherited beans, in order to keep
the beans in compliance with the EJB specification. Such synchronization may
not occur if you change the source in the Source page.