JBoss.org Community Documentation

12.5.3.1. Relationship Role Mapping

Each of the two ejb-relationship-role elements contains mapping information specific to an entity in the relationship. The content model of the ejb-relationship-role element is shown in Figure 12.8, “The jbosscmp-jdbc ejb-relationship-role element content model” .

The jbosscmp-jdbc ejb-relationship-role element content model

Figure 12.8. The jbosscmp-jdbc ejb-relationship-role element content model


A detailed description of the main elements follows:

As noted above, the key-fields element contains a key-field for each primary key field of the current entity. The key-field element uses the same syntax as the cmp-field element of the entity, except that key-field does not support the not-null option. Key fields of a relation-table are automatically not null, because they are the primary key of the table. On the other hand, foreign key fields must be nullable by default. This is because the CMP specification requires an insert into the database after the ejbCreate method and an update to it after to pick up CMR changes made in ejbPostCreate. Since the EJB specification does not allow a relationship to be modified until ejbPostCreate, a foreign key will be initially set to null. There is a similar problem with removal. You can change this insert behavior using the jboss.xml insert-after-ejb-post-create container configuration flag. The following example illustrates the creation of a new bean configuration that uses insert-after-ejb-post-create by default.

<jboss>
    <!-- ... -->
    <container-configurations>
        <container-configuration extends="Standard CMP 2.x EntityBean">
            <container-name>INSERT after ejbPostCreate Container</container-name>
            
               <insert-after-ejb-post-create>true</insert-after-ejb-post-create>
            
        </container-configuration>
    </container-configurations>                     
</jboss>

An alternate means of working around the non-null foreign key issue is to map the foreign key elements onto non-null CMP fields. In this case you simply populate the foreign key fields in ejbCreate using the associated CMP field setters.

The content model of the key-fields element is Figure 12.9, “The jbosscmp-jdbc key-fields element content model”.

The jbosscmp-jdbc key-fields element content model

Figure 12.9. The jbosscmp-jdbc key-fields element content model


A detailed description of the elements contained in the key-field element follows:

  • field-name : This required element identifies the field to which this mapping applies. This name must match a primary key field of the current entity.

  • column-name : Use this element to specify the column name in which this primary key field will be stored. If this is relationship uses foreign-key-mapping, this column will be added to the table for the related entity. If this relationship uses relation-table-mapping, this column is added to the relation-table. This element is not allowed for mapped dependent value class; instead use the property element.

  • jdbc-type : This is the JDBC type that is used when setting parameters in a JDBC PreparedStatement or loading data from a JDBC ResultSet. The valid types are defined in java.sql.Types.

  • sql-type : This is the SQL type that is used in create table statements for this field. Valid types are only limited by your database vendor.

  • property : Use this element for to specify the mapping of a primary key field which is a dependent value class.

  • dbindex : The presence of this optional field indicates that the server should create an index on the corresponding column in the database, and the index name will be fieldname_index.