Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "Configuring a Relational Many-to-Many Mapping (ELUG)"
m (→Introduction to Relational Many-to-Many Mapping Configuration) |
m |
||
Line 137: | Line 137: | ||
For more information, see the following: | For more information, see the following: | ||
− | * [[Introduction%20to%20Relational%20Mappings%20(ELUG)|Many-to-Many Mapping]] | + | * [[Introduction%20to%20Relational%20Mappings%20(ELUG)#Many-to-Many Mapping|Many-to-Many Mapping]] |
* [[Configuring%20a%20Relational%20Mapping%20(ELUG)|Configuring a Relational Mapping]] | * [[Configuring%20a%20Relational%20Mapping%20(ELUG)|Configuring a Relational Mapping]] | ||
* [[Creating%20and%20Configuring%20Mappings%20(ELUG)|Creating and Configuring Mappings]] | * [[Creating%20and%20Configuring%20Mappings%20(ELUG)|Creating and Configuring Mappings]] | ||
− | For information on using JPA to configure many-to-many mappings, see [[Introduction%20to%20EclipseLink%20JPA%20(ELUG)|@ManyToMany]]. | + | For information on using JPA to configure many-to-many mappings, see [[Introduction%20to%20EclipseLink%20JPA%20(ELUG)#@ManyToMany|@ManyToMany]]. |
+ | |||
+ | |||
==Configuring a Relation Table== | ==Configuring a Relation Table== | ||
− | The relation table contains the columns for the primary keys of the source table and target table involved in the many-to-many mapping. You must create this table in the database before completing the mapping. See [[Using%20Workbench%20(ELUG)|Using Databases]] for information on creating database tables. | + | The relation table contains the columns for the primary keys of the source table and target table involved in the many-to-many mapping. You must create this table in the database before completing the mapping. See [[Using%20Workbench%20(ELUG)#Using Databases|Using Databases]] for information on creating database tables. |
In [[Introduction%20to%20Relational%20Mappings%20(ELUG)#Figure 32-5|Figure 32-5]], the <tt>PROJ_EMP</tt> table serves as the relation table between the <tt>PROJECT</tt> and <tt>EMPLOYEE</tt> tables. | In [[Introduction%20to%20Relational%20Mappings%20(ELUG)#Figure 32-5|Figure 32-5]], the <tt>PROJ_EMP</tt> table serves as the relation table between the <tt>PROJECT</tt> and <tt>EMPLOYEE</tt> tables. | ||
Line 157: | Line 159: | ||
# Select the mapped attribute in the '''Navigator'''. Its properties appear in the Editor. | # Select the mapped attribute in the '''Navigator'''. Its properties appear in the Editor. | ||
− | # Click the '''General''' tab. The General tab appears.<br>''''' | + | # Click the '''General''' tab. The General tab appears.<br><span id="Figure 39-1"></span>''''' Table Reference Tab, Relation Table Option'''''<br>[[Image:mmreltabl.gif|Table Reference Tab, Relation Table Option]] |
Use the '''Relation Table''' field to select a database table to define this mapping. | Use the '''Relation Table''' field to select a database table to define this mapping. | ||
Line 196: | Line 198: | ||
In addition to the API that [[#Example 39-1|Configuring a Relational Table]] illustrates, other common API for use with many-to-many mappings include the following: | In addition to the API that [[#Example 39-1|Configuring a Relational Table]] illustrates, other common API for use with many-to-many mappings include the following: | ||
− | |||
− | |||
− | |||
* <tt>useBasicIndirection</tt><nowiki>: implements EclipseLink value holder indirection.</nowiki> | * <tt>useBasicIndirection</tt><nowiki>: implements EclipseLink value holder indirection.</nowiki> | ||
* <tt>useTransparentCollection</tt><nowiki>: if you use transparent indirection, this element places a special collection in the source object's attribute.</nowiki> | * <tt>useTransparentCollection</tt><nowiki>: if you use transparent indirection, this element places a special collection in the source object's attribute.</nowiki> | ||
Line 204: | Line 203: | ||
For more information about the available methods for <tt>ManyToManyMapping</tt>, see the ''EclipseLink API Reference''. | For more information about the available methods for <tt>ManyToManyMapping</tt>, see the ''EclipseLink API Reference''. | ||
+ | |||
+ | |||
Revision as of 20:10, 29 November 2007
Contents
This section describes the various components that you must configure in order to use a relational many-to-many mapping.
For information on how to configure EclipseLink mappings options common to two or more mapping types, see Configuring a Mapping.
For information on how to create EclipseLink mappings, see Creating a Mapping.
Configurable Options for Relational Many-to-Many Mapping lists the configurable options for a relational many-to-many mapping.
Introduction to Relational Many-to-Many Mapping Configuration
This table lists the configurable options for a relational many-to-many mapping.
Configurable Options for Relational Many-to-Many Mapping
Option to Configure | Workbench | Java |
---|---|---|
Reference descriptor (see Configuring Reference Descriptor) |
||
Method or direct field access (see Configuring a Type Conversion Converter) |
||
Read-only mapping (see Configuring Read-Only Mappings) |
||
Private or Independent relationships (see Configuring Mapping Comments) |
||
Batch reading (see Configuring Batch Reading) |
||
Indirection (lazy loading) (see Configuring Mapping Comments) |
||
Bidirectional relationship (see Configuring Bidirectional Relationship) |
||
Container policy (see Configuring Mapping Comments) |
||
Mapping comments (see Configuring Mapping Comments) |
||
Relational table (see #Configuring a Relation Table) |
||
Table and field references (see Configuring Joining at the Mapping Level) (Source) |
||
Table and field references (see Configuring Joining at the Mapping Level) (Target) |
||
Query key order (see Configuring Query Key Order) |
This example shows how to create a many-to-many mapping and add it to a descriptor using Java code.
Many-to-Many Mapping
public void customize(ClassDescriptor descriptor) { ManyToManyMapping mapping = new ManyToManyMapping(); // configure mapping ... // add mapping to descriptor descriptor.addMapping(mapping); }
For more information, see the following:
For information on using JPA to configure many-to-many mappings, see @ManyToMany.
Configuring a Relation Table
The relation table contains the columns for the primary keys of the source table and target table involved in the many-to-many mapping. You must create this table in the database before completing the mapping. See Using Databases for information on creating database tables.
In Figure 32-5, the PROJ_EMP table serves as the relation table between the PROJECT and EMPLOYEE tables.
How to Configure a Relation Table Using Workbench
To select a relation table for a mapping, use this procedure:
- Select the mapped attribute in the Navigator. Its properties appear in the Editor.
- Click the General tab. The General tab appears.
Table Reference Tab, Relation Table Option
Use the Relation Table field to select a database table to define this mapping.
How to Configure a Relation Table Using Java
Many-to-many mappings represent the relationships between a collection of source objects and a collection of target objects. This requires an intermediate table that manages the associations between the source and target records.
Many-to-many mappings are instances of the ManyToManyMapping class and requires the following elements:
- The attribute mapped, set by using the setAttributeName method.
- The reference class, set by using the setReferenceClass method.
- The relation table, set by using the setRelationTableName() method.
- The foreign key information (for noncomposite target primary keys), which you specify by calling the setSourceRelationKeyFieldName and setTargetRelationKeyFieldName methods.
- The foreign key information if the source or target primary keys are composite, which you specify by sending the addSourceRelationKeyFieldName or addTargetRelationKeyFieldName methods.
Configuring a Relational Table
public void customize(ClassDescriptor descriptor) { // In the Employee class, create the mapping that references Project class ManyToManyMapping manyToManyMapping = new ManyToManyMapping(); manyToManyMapping.setAttributeName("projects"); manyToManyMapping.setReferenceClass(Project.class); // Configure the relational table manyToManyMapping.setRelationTableName("PROJ_EMP"); manyToManyMapping.setSourceRelationKeyFieldName ("EMPID"); manyToManyMapping.setTargetRelationKeyFieldName ("PROJID"); // Add mapping to descriptor descriptor.addMapping(manyToManyMapping); }
In addition to the API that Configuring a Relational Table illustrates, other common API for use with many-to-many mappings include the following:
- useBasicIndirection: implements EclipseLink value holder indirection.
- useTransparentCollection: if you use transparent indirection, this element places a special collection in the source object's attribute.
- dontUseIndirection: implements no indirection.
For more information about the available methods for ManyToManyMapping, see the EclipseLink API Reference.