Mapping Relationship Fields

See Also

When you have foreign-key relationships between database tables, you usually want to preserve those relationships in the CMP beans mapped to those tables. Container-managed relationships (CMR) determine how relationships between mapped CMP beans are stored in the database. CMRs enable navigation from one bean to another, in the same way that a foreign key enables navigation from one table to another.

When mapping relationship fields, you typically use a foreign key to connect your tables. If there is no foreign key, then you must create key column pairs to act as the key between tables. The columns in each pair are expected to have the same value.

If you generate CMP entity beans from a database schema, the IDE generates the necessary relationships in the deployment descriptor for you based on the database schema. If you are you are generating CMP entity beans from scratch and need to configure CMRs for your application, you need to edit the deployment descriptor file in the XML editor.

If you are deploying to the GlassFish Server, you configure CMR fields in the sun-cmp-mappings.xml. If you are deploying to a different application server, you have to configure CMR fields yourself in the server-specific deployment descriptors.

To edit CMR field mappings:

  1. Make sure you have configured ejb-jar.xml to specify your data source, and the CMP relationships and that you have specified the primary and any secondary tables.
  2. In the Projects window, expand the Configuration Files node for your EJB module and double-click sun-cmp-mappings.xml.
  3. Edit the XML to create the necessary fields. You can use the IDE's code completion to help you edit the deployment descriptor properties.
  4. Validate the XML file.
  5. Update your source code where necessary.
See Also
Configuring CMP Mappings
Managing Container-Managed Relationships
Generating CMP Entity Beans from a Database
About EJB 2.1 CMP Mappings

Legal Notices