31.5.3. Relationship Mapping
relationshipssection of the
ejb-relationelements. Relationships are identified by the
ejb-relationelement content model is shown in Figure 31.7, “The jbosscmp-jdbc.xml ejb-relation element content model”.
Figure 31.7. The jbosscmp-jdbc.xml ejb-relation element content model
<jbosscmp-jdbc> <relationships> <ejb-relation> <ejb-relation-name>Organization-Gangster</ejb-relation-name> <foreign-key-mapping/> <ejb-relationship-role> <ejb-relationship-role-name>org-has-gangsters</ejb-relationship-role-name> <key-fields> <key-field> <field-name>name</field-name> <column-name>organization</column-name> </key-field> </key-fields> </ejb-relationship-role> <ejb-relationship-role> <ejb-relationship-role-name>gangster-belongs-to-org</ejb-relationship-role-name> <key-fields/> </ejb-relationship-role> </ejb-relation> </relationships> </jbosscmp-jdbc>
ejb-relation-nameof the relationship being mapped is declared, the relationship can be declared as read only using the
read-time-outelements. They have the same semantics as their counterparts in the entity element.
ejb-relationelement must contain either a
foreign-key-mappingelement or a
relation-table-mappingelement, which are described in Section 188.8.131.52, “Foreign Key Mapping” and Section 184.108.40.206, “Relation table Mapping”. This element may also contain a pair of
ejb-relationship-roleelements as described in the following section.
220.127.116.11. Relationship Role Mapping
ejb-relationship-roleelements contains mapping information specific to an entity in the relationship. The content model of the
ejb-relationship-roleelement is shown in Figure 31.8, “The jbosscmp-jdbc ejb-relationship-role element content model”.
Figure 31.8. The jbosscmp-jdbc ejb-relationship-role element content model
- ejb-relationship-role-name: This required element gives the name of the role to which this configuration applies. It must match the name of one of the roles declared for this relationship in the
- fk-constraint: This optional element is a true/false value that indicates whether JBoss should add a foreign key constraint to the tables for this side of the relationship. JBoss will only add generate the constraint if both the primary table and the related table were created by JBoss during deployment.
- key-fields: This optional element specifies the mapping of the primary key fields of the current entity, whether it is mapped in the relation table or in the related object. The
key-fieldselement must contain a
key-fieldelement for each primary key field of the current entity. The
key-fieldselement can be empty if no foreign key mapping is needed for this side of the relation. An example of this would be the many side of a one-to-many relationship. The details of this element are described below.
- read-ahead: This optional element controls the caching of this relationship. This option is discussed in Section 18.104.22.168, “Relationships”.
- batch-cascade-delete: This indicates that a cascade delete on this relationship should be performed with a single SQL statement. This requires that the relationship be marked as
key-fieldselement contains a
key-fieldfor each primary key field of the current entity. The
key-fieldelement uses the same syntax as the
cmp-fieldelement of the entity, except that
key-fielddoes not support the
not-nulloption. Key fields of a
relation-tableare 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
ejbCreatemethod 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
insert-after-ejb-post-createcontainer configuration flag. The following example illustrates the creation of a new bean configuration that uses
<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>
ejbCreateusing the associated CMP field setters.
Figure 31.9. The jbosscmp-jdbc key-fields element content model
- 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
PreparedStatementor loading data from a JDBC ResultSet. The valid types are defined in
- 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