You can specify the mapping to the database of Entity Beans and their relationships in the JOnAS-specific deployment descriptor, in jonas-entity elements, and in jonas-ejb-relation elements. Since JOnAS is able to generate the database mapping, all the elements of the JOnAS-specific deployment descriptor defined in this section (which are sub-elements of jonas-entity or jonas-ejb-relation) are optional, except those for specifying the datasource and the initialization mode (that is, the jndi-name of jdbc-mapping and cleanup). The default values of these mapping elements, provided in this section, define the JOnAS-generated database mapping.
To specify the database within which a CMP 2.0 Entity Bean is stored, use the jndi-name element of the jdbc-mapping. This is the JNDI name of the DataSource representing the database storing the Entity Bean.
<jdbc-mapping> <jndi-name>jdbc_1</jndi-name> </jdbc-mapping> |
For a CMP 2.0 Entity Bean, the JOnAS-specific deployment descriptor contains an additional element, cleanup, to be specified before the jdbc-mapping element. The cleanup element can have one of the following values:
At bean loading time, delete the content of the tables storing the bean data
At bean loading time, drop the tables storing the bean data (if they exist) and re-create them
Do nothing
Default value (if the element is not specified). At bean loading time, create the tables for storing the bean data (if they do not exist).
It may be useful for testing purposes to delete the database data each time a bean is loaded. To do this, the part of the JOnAS-specific deployment descriptor related to the Entity Bean may look like the following:
<cleanup>removedata</cleanup> <jdbc-mapping> <jndi-name>jdbc_1</jndi-name> </jdbc-mapping> |
Mapping CMP fields in CMP2.0 is similar to that of CMP1.1, but in CMP2.0 it is also possible to specify the SQL type of a column. Usually this SQL type is used if JOnAS creates the table (the create value of the cleanup element) and if the JORM SQL type is not appropriate.
..... <entity> <ejb-name>A</ejb-name> ..... <cmp-field> <field-name>idA</field-name> </cmp-field> <cmp-field> <field-name>f</field-name> </cmp-field> ..... </entity> ..... |
..... <jonas-entity> <ejb-name>A</ejb-name> ..... <jdbc-mapping> <jndi-name>jdbc_1</jndi-name> <jdbc-table-name>t_A</jdbc-table-name> <cmp-field-jdbc-mapping> <field-name>idA</field-name> <jdbc-field-name>c_idA</jdbc-field-name> </cmp-field-jdbc-mapping> <cmp-field-jdbc-mapping> <field-name>f</field-name> <jdbc-field-name>c_f</jdbc-field-name> <sql-type>varchar(40)</sql-type> </cmp-field-jdbc-mapping> </jdbc-mapping> ..... </jonas-entity> ..... |
jndi-name | Mandatory |
jdbc-table-name | Optional. Default value is the upper-case CMP2 abstract-schema-name, or the CMP1 EJB-name, suffixed by "_". |
cmp-field-jdbc-mapping | Optional. |
jdbc-field-name | Optional. Default value is the field-name suffixed by "_". "idA_" and "f_" in the example. |
sql-type | Optional. Default value defined by JORM. |
Table 8-1. CMP fields mapping: Default values
..... <entity> <ejb-name>A</ejb-name> ..... <cmp-field> <field-name>idA</field-name> </cmp-field> <primkey-field>idA</primkey-field> ..... </entity> ..... <entity> <ejb-name>B</ejb-name> ..... <cmp-field> <field-name>idB</field-name> </cmp-field> <primkey-field>idB</primkey-field> ..... </entity> ..... <relationships> <ejb-relation> <ejb-relation-name>a-b</ejb-relation-name> <ejb-relationship-role> <!-- A => B --> <ejb-relationship-role-name>a2b</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> </ejb-relationship-role> <ejb-relationship-role> <!-- B => A --> <ejb-relationship-role-name>b2a</ejb-relationship-role-name> <multiplicity>One</multiplicity> <relationship-role-source> <ejb-name>B</ejb-name> </relationship-role-source> </ejb-relationship-role> </ejb-relation> </relationships> ..... |
There is a foreign key in the table of the bean that owns the CMR field.
..... <jonas-entity> <ejb-name>A</ejb-name> ..... <jdbc-mapping> <jndi-name>jdbc_1</jndi-name> <jdbc-table-name>t_A/jdbc-table-name> <cmp-field-jdbc-mapping> <field-name>idA</field-name> <jdbc-field-name>c_idA</jdbc-field-name> </cmp-field-jdbc-mapping> </jdbc-mapping> ..... </jonas-entity> ..... <jonas-entity> <ejb-name>B</ejb-name> ..... <jdbc-mapping> <jndi-name>jdbc_1</jndi-name> <jdbc-table-name>t_B/jdbc-table-name> <cmp-field-jdbc-mapping> <field-name>idB</field-name> <jdbc-field-name>c_idB</jdbc-field-name> </cmp-field-jdbc-mapping> </jdbc-mapping> ..... </jonas-entity> ..... <jonas-ejb-relation> <ejb-relation-name>a-b</ejb-relation-name> <jonas-ejb-relationship-role> <ejb-relationship-role-name>a2b</ejb-relationship-role-name> <foreign-key-jdbc-mapping> <foreign-key-jdbc-name>cfk_idb</foreign-key-jdbc-name> </foreign-key-jdbc-mapping> </jonas-ejb-relationship-role> </jonas-ejb-relation> ..... |
foreign-key-jdbc-name is the column name of the foreign key in the table of the source bean of the relationship-role.
In this example, where the destination bean has a primary-key-field, it is possible to deduce that this foreign-key-jdbc-name column is to be associated with the column of this primary-key-field in the table of the destination bean.
In contrast to 1-1 unidirectional relationships, there is a CMR field in both of the beans, thus making two types of mapping possible.
..... <relationships> <ejb-relation> <ejb-relation-name>a-b</ejb-relation-name> <ejb-relationship-role> <!-- A => B --> <ejb-relationship-role-name>a2b</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> </ejb-relationship-role> <ejb-relationship-role> <!-- B => A --> <ejb-relationship-role-name>b2a</ejb-relationship-role-name> <multiplicity>One</multiplicity> <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-relation> </relationships> ..... |
Two mappings are possible. One of the tables may hold a foreign key.
Case 1:
Case 2:
Case 1:
..... <jonas-ejb-relation> <ejb-relation-name>a-b</ejb-relation-name> <jonas-ejb-relationship-role> <ejb-relationship-role-name>a2b</ejb-relationship-role-name> <foreign-key-jdbc-mapping> <foreign-key-jdbc-name>cfk_idb</foreign-key-jdbc-name> </foreign-key-jdbc-mapping> </jonas-ejb-relationship-role> </jonas-ejb-relation> ..... |
Case 2:
..... <jonas-ejb-relation> <ejb-relation-name>a-b</ejb-relation-name> <jonas-ejb-relationship-role> <ejb-relationship-role-name>b2a</ejb-relationship-role-name> <foreign-key-jdbc-mapping> <foreign-key-jdbc-name>cfk_ida</foreign-key-jdbc-name> </foreign-key-jdbc-mapping> </jonas-ejb-relationship-role> </jonas-ejb-relation> ..... |
For the default mapping, the foreign key is in the table of the source bean of the first ejb-relationship-role of the ejb-relation. In the example, the default mapping corresponds to case 1, since the ejb-relationship-role a2b is the first defined in the ejb-relation a-b. Then, the default values are similar to those of the 1-1 unidirectional relationship.
..... <relationships> <ejb-relation> <ejb-relation-name>a-b</ejb-relation-name> <ejb-relationship-role> <!-- A => B --> <ejb-relationship-role-name>a2b</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-relationship-role> <!-- B => A --> <ejb-relationship-role-name>b2a</ejb-relationship-role-name> <multiplicity>Many</multiplicity> <relationship-role-source> <ejb-name>B</ejb-name> </relationship-role-source> </ejb-relationship-role> </ejb-relation> </relationships> ..... |
In this case, the foreign key must be in the table of the bean which is on the "many" side of the relationship (that is, in the table of the source bean of the relationship role with multiplicity many), t_B.
..... <jonas-ejb-relation> <ejb-relation-name>a-b</ejb-relation-name> <jonas-ejb-relationship-role> <ejb-relationship-role-name>b2a</ejb-relationship-role-name> <foreign-key-jdbc-mapping> <foreign-key-jdbc-name>cfk_ida</foreign-key-jdbc-name> </foreign-key-jdbc-mapping> </jonas-ejb-relationship-role> </jonas-ejb-relation> ..... |
jonas-ejb-relation | Optional |
foreign-key-jdbc-name | Optional. Default value is the abstract-schema-name of the destination bean of the "one" side of the relationship (that is, the source bean of the relationship role with multiplicity one) suffixed by "_" and by its primary-key-field. A_ida in the example. |
Table 8-3. 1-N unidirectional relationships: Default values
Similar to 1-N unidirectional relationships, but with a CMR field in each bean.
..... <relationships> <ejb-relation> <ejb-relation-name>a-b</ejb-relation-name> <ejb-relationship-role> <!-- A => B --> <ejb-relationship-role-name>a2b</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-relationship-role> <!-- B => A --> <ejb-relationship-role-name>b2a</ejb-relationship-role-name> <multiplicity>Many</multiplicity> <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-relation> </relationships> ..... |
In this case, the foreign key must be in the table of the bean that is on the "many" side of the relationship (that is, in the table of the source bean of the relationship role with multiplicity many), t_B.
..... <jonas-ejb-relation> <ejb-relation-name>a-b</ejb-relation-name> <jonas-ejb-relationship-role> <ejb-relationship-role-name>b2a</ejb-relationship-role-name> <foreign-key-jdbc-mapping> <foreign-key-jdbc-name>cfk_ida</foreign-key-jdbc-name> </foreign-key-jdbc-mapping> </jonas-ejb-relationship-role> </jonas-ejb-relation> ..... |
jonas-ejb-relation | Optional |
foreign-key-jdbc-name | Optional. Default value is the abstract-schema-name of the destination bean of the "one" side of the relationship (that is, the source bean of the relationship role with multiplicity one), suffixed by "_" and its primary-key-field. A_ida in the example. |
Table 8-4. 1-N bidirectional relationships: Default values
Similar to 1-N unidirectional relationships, but the CMR field is defined on the "many" side of the relationship, that is, on the (source bean of the) relationship role with multiplicity "many."
..... <relationships> <ejb-relation> <ejb-relation-name>a-b</ejb-relation-name> <ejb-relationship-role> <!-- A => B --> <ejb-relationship-role-name>a2b</ejb-relationship-role-name> <multiplicity>Many</multiplicity> <relationship-role-source> <ejb-name>A</ejb-name> </relationship-role-source> <cmr-field> <cmr-field-name>b</cmr-field-name> </cmr-field> </ejb-relationship-role> <ejb-relationship-role> <!-- B => A --> <ejb-relationship-role-name>b2a</ejb-relationship-role-name> <multiplicity>One</multiplicity> <relationship-role-source> <ejb-name>B</ejb-name> </relationship-role-source> </ejb-relationship-role> </ejb-relation> </relationships> ..... |
In this case, the foreign key must be in the table of the bean which is on the "many" side of the relationship (that is, in table of the source bean of the relationship role with multiplicity many), t_A.
..... <jonas-ejb-relation> <ejb-relation-name>a-b</ejb-relation-name> <jonas-ejb-relationship-role> <ejb-relationship-role-name>a2b</ejb-relationship-role-name> <foreign-key-jdbc-mapping> <foreign-key-jdbc-name>cfk_idb</foreign-key-jdbc-name> </foreign-key-jdbc-mapping> </jonas-ejb-relationship-role> </jonas-ejb-relation> ..... |
jonas-ejb-relation | Optional |
foreign-key-jdbc-name | Optional. Default value is the abstract-schema-name of the destination bean of the "one" side of the relationship (that is, the source bean of the relationship role with multiplicity one) suffixed by "_" and by its primary-key-field. B_idb in the example. |
Table 8-5. N-1 unidirectional relationships: Default values
..... <relationships> <ejb-relation> <ejb-relation-name>a-b</ejb-relation-name> <ejb-relationship-role> <!-- A => B --> <ejb-relationship-role-name>a2b</ejb-relationship-role-name> <multiplicity>Many</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-relationship-role> <!-- B => A --> <ejb-relationship-role-name>b2a</ejb-relationship-role-name> <multiplicity>Many</multiplicity> <relationship-role-source> <ejb-name>B</ejb-name> </relationship-role-source> </ejb-relationship-role> </ejb-relation> </relationships> ..... |
In this case, there is a join table composed of the foreign keys of each Entity Bean table.
..... <jonas-ejb-relation> <ejb-relation-name>a-b</ejb-relation-name> <jdbc-table-name>tJoin_AB</jdbc-table-name> <jonas-ejb-relationship-role> <ejb-relationship-role-name>a2b</ejb-relationship-role-name> <foreign-key-jdbc-mapping> <foreign-key-jdbc-name>cfk_idb</foreign-key-jdbc-name> </foreign-key-jdbc-mapping> </jonas-ejb-relationship-role> <jonas-ejb-relationship-role> <ejb-relationship-role-name>b2a</ejb-relationship-role-name> <foreign-key-jdbc-mapping> <foreign-key-jdbc-name>cfk_ida</foreign-key-jdbc-name> </foreign-key-jdbc-mapping> </jonas-ejb-relationship-role> </jonas-ejb-relation> ..... |
jonas-ejb-relation | Optional |
jdbc-table-name | Optional. Default value is built from the abstract-schema-names of the beans, separated by _. A_B in the example. |
foreign-key-jdbc-name | Optional. Default value is the abstract-schema-name of the destination bean, suffixed by "_" and by its primary-key-field. B_idb and A_ida in the example. |
Table 8-6. N-M unidirectional relationships: Default values
Similar to N-M unidirectional relationships, but a CMR field is defined for each bean.
..... <relationships> <ejb-relation> <ejb-relation-name>a-b</ejb-relation-name> <ejb-relationship-role> <!-- A => B --> <ejb-relationship-role-name>a2b</ejb-relationship-role-name> <multiplicity>Many</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-relationship-role> <!-- B => A --> <ejb-relationship-role-name>b2a</ejb-relationship-role-name> <multiplicity>Many</multiplicity> <relationship-role-source> <ejb-name>B</ejb-name> </relationship-role-source> <cmr-field> <cmr-field-name>a</cmr-field-name> <cmr-field-type>java.util.Collection</cmr-field-type> </cmr-field> </ejb-relationship-role> </ejb-relation> </relationships> ..... |
In this case, there is a join table composed of the foreign keys of each Entity Bean table.
..... <jonas-ejb-relation> <ejb-relation-name>a-b</ejb-relation-name> <jdbc-table-name>tJoin_AB</jdbc-table-name> <jonas-ejb-relationship-role> <ejb-relationship-role-name>a2b</ejb-relationship-role-name> <foreign-key-jdbc-mapping> <foreign-key-jdbc-name>cfk_idb</foreign-key-jdbc-name> </foreign-key-jdbc-mapping> </jonas-ejb-relationship-role> <jonas-ejb-relationship-role> <ejb-relationship-role-name>b2a</ejb-relationship-role-name> <foreign-key-jdbc-mapping> <foreign-key-jdbc-name>cfk_ida</foreign-key-jdbc-name> </foreign-key-jdbc-mapping> </jonas-ejb-relationship-role> </jonas-ejb-relation> ..... |
jonas-ejb-relation | Optional |
jdbc-table-name | Optional. Default value is built from the abstract-schema-names of the beans, separated by _. A_B in the example. |
foreign-key-jdbc-name | Optional. Default value is the abstract-schema-name of the destination bean, suffixed by "_" and by its primary-key-field. B_idb and A_ida in the example. |
Table 8-7. CMR fields mapping to primary-key-fields: Default values
In the case of composite primary keys, the database mapping should provide the capability to specify which column of a foreign key corresponds to which column of the primary key. This is the only difference between relationships based on simple primary keys. For this reason, not all types of relationship are illustrated below.
..... <entity> <ejb-name>A</ejb-name> ..... <prim-key-class>p.PkA</prim-key-class> ..... <cmp-field> <field-name>id1A</field-name> </cmp-field> <cmp-field> <field-name>id2A</field-name> </cmp-field> ..... </entity> ..... <entity> <ejb-name>B</ejb-name> ..... <prim-key-class>p.PkB</prim-key-class> ..... <cmp-field> <field-name>id1B</field-name> </cmp-field> <cmp-field> <field-name>id2B</field-name> </cmp-field> ..... </entity> ..... <relationships> <ejb-relation> <ejb-relation-name>a-b</ejb-relation-name> <ejb-relationship-role> <!-- A => B --> <ejb-relationship-role-name>a2b</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> </ejb-relationship-role> <ejb-relationship-role> <!-- B => A --> <ejb-relationship-role-name>b2a</ejb-relationship-role-name> <multiplicity>One</multiplicity> <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-relation> </relationships> ..... |
Two mappings are possible, one or another of the tables may hold the foreign key.
Case 1:
Case 2:
Case 1:
..... <jonas-ejb-relation> <ejb-relation-name>a-b</ejb-relation-name> <jonas-ejb-relationship-role> <ejb-relationship-role-name>a2b</ejb-relationship-role-name> <foreign-key-jdbc-mapping> <foreign-key-jdbc-name>cfk_id1b</foreign-key-jdbc-name> <key-jdbc-name>c_id1b</key-jdbc-name> </foreign-key-jdbc-mapping> <foreign-key-jdbc-mapping> <foreign-key-jdbc-name>cfk_id2b</foreign-key-jdbc-name> <key-jdbc-name>c_id2b</key-jdbc-name> </foreign-key-jdbc-mapping> </jonas-ejb-relationship-role> </jonas-ejb-relation> ..... |
Case 2:
..... <jonas-ejb-relation> <ejb-relation-name>a-b</ejb-relation-name> <jonas-ejb-relationship-role> <ejb-relationship-role-name>b2a</ejb-relationship-role-name> <foreign-key-jdbc-mapping> <foreign-key-jdbc-name>cfk_id1a</foreign-key-jdbc-name> <key-jdbc-name>c_id1a</key-jdbc-name> </foreign-key-jdbc-mapping> <foreign-key-jdbc-mapping> <foreign-key-jdbc-name>cfk_id2a</foreign-key-jdbc-name> <key-jdbc-name>c_id2a</key-jdbc-name> </foreign-key-jdbc-mapping> </jonas-ejb-relationship-role> </jonas-ejb-relation> ..... |
For the default mapping (values), the foreign key is in the table of the source bean of the first ejb-relationship-role of the ejb-relation. In the example, the default mapping corresponds to case 1, since the ejb-relationship-role a2b is the first defined in the ejb-relation a-b.
..... <entity> <ejb-name>A</ejb-name> ..... <cmp-field> <field-name>id1A</field-name> </cmp-field> <cmp-field> <field-name>id2A</field-name> </cmp-field> ..... </entity> ..... <entity> <ejb-name>B</ejb-name> ..... <cmp-field> <field-name>id1B</field-name> </cmp-field> <cmp-field> <field-name>id2B</field-name> </cmp-field> ..... </entity> ..... <relationships> <ejb-relation> <ejb-relation-name>a-b</ejb-relation-name> <ejb-relationship-role> <!-- A => B --> <ejb-relationship-role-name>a2b</ejb-relationship-role-name> <multiplicity>Many</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-relationship-role> <!-- B => A --> <ejb-relationship-role-name>b2a</ejb-relationship-role-name> <multiplicity>Many</multiplicity> <relationship-role-source> <ejb-name>B</ejb-name> </relationship-role-source> </ejb-relationship-role> </ejb-relation> </relationships> ..... |
In this case, there is a join table composed of the foreign keys of each Entity Bean table.
..... <jonas-ejb-relation> <ejb-relation-name>a-b</ejb-relation-name> <jdbc-table-name>tJoin_AB</jdbc-table-name> <jonas-ejb-relationship-role> <ejb-relationship-role-name>a2b</ejb-relationship-role-name> <foreign-key-jdbc-mapping> <foreign-key-jdbc-name>cfk_id1b</foreign-key-jdbc-name> <key-jdbc-name>c_id1b</key-jdbc-name> </foreign-key-jdbc-mapping> <foreign-key-jdbc-mapping> <foreign-key-jdbc-name>cfk_id2b</foreign-key-jdbc-name> <key-jdbc-name>c_id2b</key-jdbc-name> </foreign-key-jdbc-mapping> </jonas-ejb-relationship-role> <jonas-ejb-relationship-role> <ejb-relationship-role-name>b2a</ejb-relationship-role-name> <foreign-key-jdbc-mapping> <foreign-key-jdbc-name>cfk_id1a</foreign-key-jdbc-name> <key-jdbc-name>c_id1a</key-jdbc-name> </foreign-key-jdbc-mapping> <foreign-key-jdbc-mapping> <foreign-key-jdbc-name>cfk_id2a</foreign-key-jdbc-name> <key-jdbc-name>c_id2a</key-jdbc-name> </foreign-key-jdbc-mapping> </jonas-ejb-relationship-role> </jonas-ejb-relation> ..... |