Skip Headers
Oracle® Application Server TopLink Mapping Workbench User's Guide
10g Release 2 (10.1.2)
Part No. B15900-01
  Go To Documentation Library
Home
Go To Product List
Solution Area
Go To Table Of Contents
Contents
Go To Index
Index

Previous
Previous
Next
Next
 

Working with Custom Relationship Mappings

Just as a descriptor's query manager generates the default SQL code used for database interaction, relationship mappings also generate query information.

As with the queries used by a descriptor's query manager, you can customize relationship mappings using SQL strings or query objects. Refer to "Specifying Queries and Named Finders" on page 4-12 for more information on customizing queries and the syntax that OracleAS TopLink supports.

To customize the way a relationship mapping generates SQL, use any of the following methods:

A query object that specifies the search criteria must be passed to each of these methods. Because search criteria for these operations usually depend on variables at runtime, the query object must usually be created from a parameterized expression, SQL string, or stored procedure call.

See the Oracle Application Server TopLink Application Developer's Guide for more information on defining parameterized queries and stored procedure calls.

Creating Custom Mapping Queries in Java Code

The following example illustrates selection customization with a parameterized expression using setSelectionCriteria(), and deletion customization using setDeleteAllSQLString(). Because the descriptor is passed as the parameter to this amendment method, which has been specified to be called after the descriptor is loaded in the project, you must locate each mapping for which you wish to define a custom query.

Example 6-7 Custom Mapping Example

The following code illustrates adding a custom query to two different mappings in the Employee descriptor.

// Amendment method in Employee class
public static void addToDescriptor(Descriptor descriptor)
{

//Find the one-to-one mapping for the address attribute
OneToOneMapping addressMapping=(OneToOneMapping) descriptor.getMappingForAttributeName("homeaddress");

//Create a parameterized Expression and register it as the default selection criterion for the mapping.
ExpressionBuilder builder = new ExpressionBuilder();
addressMapping.setSelectionCriteria(builder.getField("ADDRESS.ADDRESS_ID").equal(builder.getParameter("EMP.ADDRESS_ID")).and(builder.getField("ADDRESS.TYPE").equal("home")));

// Get the direct collection mapping for responsibilitiesList. 
DirectCollectionMapping directCollection=(DirectCollectionMapping) descriptor.getMappingForAttributeName("responsibilitiesList");
directCollection.setDeleteAllSQLString("DELETE FROM RESPONS WHERE EMP_ID = #EMP_ID");
}