Oracle® Application Server TopLink Mapping Workbench User's Guide
10g Release 2 (10.1.2) Part No. B15900-01 |
|
Previous |
Next |
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:
selection — All relationship mappings can use the setSelectionCriteria()
, setSelectionSQLString()
, and setCustomSelectionQuery()
methods of the mapping to customize the selection criteria.
insert — Many-to-many and direct collection mappings can use the setInsertSQLString()
or setCustomInsertQuery()
methods of the mapping to customize the insertion criteria.
delete all — Many-to-many, direct collection, and one-to-many mappings can use the setDeleteAllSQLString()
and setCustomDeleteAllQuery()
methods of the mapping to customize the deletion criteria.
delete —Many-to-many mappings can use the setDeleteSQLString()
and setCustomDeleteQuery()
methods of the mapping to customize the deletion criteria.
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.
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"); }