Version 12

    batch-cascade-delete

     

    Since 3.2.3

     

    The EJB2.1 spec defines the cascade-delete protocol in "10.3.4.2 Cascade-delete" as:

     

    +"If an entity is deleted, and the cascade-delete deployment descriptor element is specified for a

    related entity bean, then the removal is cascaded to cause the removal of the related entity object or

    objects. As with the remove operation, the removal triggered by the cascade-delete option

    causes the container to invoke the ejbRemove method on the entity bean instance that is to be

    removed before the persistent representation of that entity object is removed. Once an entity has been

    removed from a relationship because of a cascaded delete, the accessor methods for any relationships to

    the entity will reflect this removal. An accessor method for a one-to-one or many-to-one relationship to

    the entity will return null; and an accessor method for a many-to-many relationship to the entity will

    return a collection from which the entity object has been removed. *After removing the entity object

    from all relationships and removing its persistent representation, the container must then cascade the

    removal to all entity beans with which the entity had been previously been in container-managed relationships

    for which the cascade-delete option was specified.*"+

     

    In JBossCMP this is implemented as:

    1. destroy relationship;

    2. synchronize the state (set foreign key fields to NULL in the database to be able to remove the parent);

    3. remove the parent instance;

    4. for each child repeat the procedure starting from 1.

     

    Setting foreign key fields to NULL in the database and one DELETE SQL statement per child is not efficient. It would be more efficient to just delete children with an SQL like 'DELETE FROM CHILD_TABLE WHERE PARENT_FOREIGN_KEY = ?' and then delete the parent. This feature is implemented and all you need to do is to include <batch-cascade-delete/> in the ejb-relationship-role in jbosscmp-jdbc.xml which has <cascade-delete/> in ejb-jar.xml.

     

    For example, if in ejb-jar.xml we have

     

    <ejb-relation>
       <ejb-relationship-role >
          <ejb-relationship-role-name>B-has-A</ejb-relationship-role-name>
          <multiplicity>Many</multiplicity>
          <cascade-delete></cascade-delete>
          <relationship-role-source >
             <ejb-name>B</ejb-name>
          </relationship-role-source>
          <cmr-field >
             <cmr-field-name>a</cmr-field-name>
          </cmr-field>
       </ejb-relationship-role>
       <ejb-relationship-role >
          <ejb-relationship-role-name>A-has-B</ejb-relationship-role-name>
          <multiplicity>One</multiplicity>
          <relationship-role-source >
             <ejb-name>A</ejb-name>
          </relationship-role-source>
          <cmr-field >
             <cmr-field-name>b</cmr-field-name>
             <cmr-field-type>java.util.Collection</cmr-field-type>
          </cmr-field>
       </ejb-relationship-role>
    </ejb-relation>
    

     

    we can setup the many side for batch cascade delete in jbosscmp-jdbc.xml as

     

    <ejb-relation>
       <ejb-relation-name>A-B</ejb-relation-name>
       <ejb-relationship-role>
          <ejb-relationship-role-name>B-has-A</ejb-relationship-role-name>
          <key-fields></key-fields>
          <batch-cascade-delete></batch-cascade-delete>
       </ejb-relationship-role>
       <ejb-relationship-role>
          <ejb-relationship-role-name>A-has-B</ejb-relationship-role-name>
          <key-fields>
             <key-field>
                <field-name>id</field-name>
                <column-name>a_fk</column-name>
             </key-field>
          </key-fields>
       </ejb-relationship-role>
    </ejb-relation>