31.5.3.3. Relation table Mapping
Relation table mapping is less common for one-to-one and one-to-many relationships, but is the only mapping style allowed for many-to-many relationships. Relation table mapping is defined using the
relation-table-mapping element, the content model of which is shown below.

Figure 31.10. The jbosscmp-jdbc relation-table-mapping element content model
The relation-table-mapping for the
Gangster-Job relationship is shown in with table mapping elements:
Example 31.1. The jbosscmp-jdbc.xml Relation-table Mapping
<jbosscmp-jdbc>
<relationships>
<ejb-relation>
<ejb-relation-name>Gangster-Jobs</ejb-relation-name>
<relation-table-mapping>
<table-name>gangster_job</table-name>
</relation-table-mapping>
<ejb-relationship-role>
<ejb-relationship-role-name>gangster-has-jobs</ejb-relationship-role-name>
<key-fields>
<key-field>
<field-name>gangsterId</field-name>
<column-name>gangster</column-name>
</key-field>
</key-fields>
</ejb-relationship-role>
<ejb-relationship-role>
<ejb-relationship-role-name>job-has-gangsters</ejb-relationship-role-name>
<key-fields>
<key-field>
<field-name>name</field-name>
<column-name>job</column-name>
</key-field>
</key-fields>
</ejb-relationship-role>
</ejb-relation>
</relationships>
</jbosscmp-jdbc>
The
relation-table-mapping element contains a subset of the options available in the entity element. A detailed description of these elements is reproduced here for convenience:
- table-name: This optional element gives the name of the table that will hold data for this relationship. The default table name is based on the entity and
cmr-fieldnames. - datasource: This optional element gives the
jndi-nameused to look up the datasource. All database connections are obtained from the datasource. Having different datasources for entities is not recommended, as it vastly constrains the domain over which finders andejbSelects can query. - datasourcemapping: This optional element allows one to specify the name of the
type-mappingto use. - create-table: This optional element if true indicates JBoss should attempt to create a table for the relationship. When the application is deployed, JBoss checks if a table already exists before creating the table. If a table is found, it is logged, and the table is not created. This option is very useful during the early stages of development when the table structure changes often.
- post-table-create: This optional element specifies an arbitrary SQL statement that should be executed immediately after the database table is created. This command is only executed if
create-tableis true and the table did not previously exist. - remove-table: This optional element if true indicates JBoss should attempt to drop the
relation-tablewhen the application is undeployed. This option is very useful during the early stages of development when the table structure changes often. - row-locking: This optional element if true indicates JBoss should lock all rows loaded in a transaction. Most databases implement this by using the
SELECT FOR UPDATEsyntax when loading the entity, but the actual syntax is determined by therow-locking-templatein thedatasource-mappingused by this entity. - pk-constraint: This optional element if true indicates JBoss should add a primary key constraint when creating tables.