EJBGen Tag Inheritance

When you develop Enterprise JavaBeans and want to set default values for ejbgen properties, you can use tag inheritance. For example, if you want the value for the max-beans-in-cache property to be 3000 for all entity beans that you develop, you can create a superclass that has this property set. You can inherit entire tags and individual attributes on a tag, and you can override an inherited tag (attribute) in a subclass if necessary.

An Example

In this example a BaseEntityBean is defined with several tags. The ejbgen:env-entry tag will be entirely inherited as well as the max-beans-in-cache attribute on the ejbgen:entity tag. A code snippet of the BaseEntityBean is given first:

 /**
 * @ejbgen:env-entry value="MyCompany" type="java.lang.String" name="CompanyName"
 * 
 * @ejbgen:entity prim-key-class="Integer" max-beans-in-cache="3000"
 *   ejb-name = "BaseEntity"
 *
 * @ejbgen:...
 */
abstract public class BaseEntityBean
  extends GenericEntityBean
  implements EntityBean
{
   //Methods you want to be inherited
   ...
}

To inherit these tags, I simply need to create a subclass of the BaseEntityBean:

/**
 * @ejbgen:entity prim-key-class="java.lang.Integer"
 *   ejb-name = "InheritingBean"
 *   table-name = "InheritingBean"
 *   abstract-schema-name = "InheritingBean"
 *
 *   ...
 */
public abstract class InheritingBean extends BaseEntityBean
{
   ...
}

The InheritingBean has inherited the max-beans-in-cache property, that is the attribute on the ejbgen:entity tag, as well as the environment entry defined for the BaseEntityBean. If a tag (attribute) needs to be overriden, this can be done by simply defining the tag (attribute) in the InheritingBean, as is shown here for max-beans-in-cache:

 * @ejbgen:entity prim-key-class="java.lang.Integer"
 *   ejb-name = "InheritingBean"
 *   table-name = "InheritingBean"
 *   abstract-schema-name = "InheritingBean"
 *   max-beans-in-cache="235" 

Resolving Inheritance Ambiguity

If you inherit tags that can appear more than once in the definition of an EJB, you must specify the id attribute on this tag to resolve ambiguity. Common examples of such tags are @ejbgen:ejb-ref, @ejbgen:env-entry, and @ejbgen:relation (also see the Related Topics section below).

For instance, let's redefine the BaseEntityBean used in the sample above to include two environment entries:

/**
 * @ejbgen:env-entry id="CompID" value="MyCompany" type="java.lang.String" name="CompanyName"
 * @ejbgen:env-entry id="DeptID" value="MyDepartment" type="java.lang.String" name="DepartmentName"
 * 
 * @ejbgen:entity prim-key-class="java.lang.Integer" ejb-name="BaseEntity" max-beans-in-cache="300" 
 *
 * @ejbgen:...
 * 
 */
abstract public class BaseEntityBean
  extends GenericEntityBean
  implements EntityBean
{
   //Methods you want to be inherited
   ...
}

Notice that in addition to MyCompany, a second environment entry MyDepartment has been added. Also notice that both tags have their id attribute set. In the definition of InheritingBean, the id value is used to refer to the proper environment entry, the DepartmentName in the example, and override its value attribute:

/**
 * @ejbgen:entity prim-key-class="java.lang.Integer"
 *   ejb-name = "InheritingBean"
 *   table-name = "InheritingBean"
 *   abstract-schema-name = "InheritingBean"
 *
 * @ejbgen:env-entry id="DeptID" value="Engineering"
* ... */ public abstract class InheritingBean extends BaseEntityBean { ... }

Related Topics

@ejbgen:ejb-local-ref Annotation

@ejbgen:ejb-ref Annotation

@ejbgen:env-entry Annotation

@ejbgen:relation Annotation

@ejbgen:resource-env-ref Annotation

@ejbgen:resource-ref Annotation