Oracle9iAS TopLink Foundation Library Guide Release 2 (9.0.3) Part Number B10064-01 |
|
Mapping enables you to relate objects in your application to data in a database. This chapter discusses how you can use Java code to implement mappings in TopLink-based applications. It discusses
For detailed descriptions of these mappings, see the Oracle9iAS TopLink Mapping Workbench Reference Guide.
Direct mappings define how a persistent object refers to objects that do not have descriptors, such as the JDK classes and primitives. There are several types of direct mappings, including
In Java, use the DirectToFieldMapping
class to create direct-to-field mappings. The mapping requires you to set the following:
setAttributeName()
message
setFieldName()
message
The optional setGetMethodName()
and setSetMethodName()
messages allow TopLink to access the attribute through user-defined methods rather than directly through the attribute. You do not have to define the accessors when using Java 2.
The Descriptor
class provides the addDirectMapping()
method that can create a new DirectToFieldMapping
, set the attribute and field name parameters, and register the mapping with the descriptor.
// Create a new mapping and register it with the descriptor. DirectToFieldMapping mapping = new DirectToFieldMapping(); mapping.setAttributeName("city"); mapping.setFieldName("CITY"); descriptor.addMapping(mapping);
This mapping example assumes persistent class has getCity()
and setCity()
methods defined.
// Create a new mapping and register it with the descriptor. DirectToFieldMapping mapping = new DirectToFieldMapping(); mapping.setAttributeName("city"); mapping.setFieldName("CITY"); mapping.setGetMethodName("getCity"); mapping.setSetMethodName("setCity"); descriptor.addMapping(mapping);
// Alternate method which does the same thing. descriptor1.addDirectMapping("city", "CITY"); descriptor2.addDirectMapping("city", "getCity",
Table 7-1 summarizes the most common public methods for direct-to-field mapping:
For a complete description of all available methods for direct-to-field mapping, see the TopLink JavaDocs.
Element | Default | Method Names |
---|---|---|
Attribute to be mapped * |
not applicable |
|
Field to be mapped * |
not applicable |
|
* Required property |
Create type conversion mappings with the TypeConversionMapping
class. The following elements are required for a type conversion mapping:
setAttributeName()
message
setFieldName()
message
setAttributeClassification()
message
setFieldClassification()
message
The optional setGetMethodName()
and setSetMethodName()
messages allow TopLink to access the attribute through user-defined methods rather than directly through the attribute. You do not have to define the accessors when using Java 2.
// Create a new mapping and register it with the descriptor. TypeConversionMapping typeConversion = new TypeConversionMapping(); typeConversion.setFieldName("J_DAY"); typeConversion.setAttributeName("joiningDate"); typeConversion.setFieldClassification(java.sql.Date.class); typeConversion.setAttributeClassification(java.util.Date.class); descriptor.addMapping(typeConversion);
Table 7-2 summarizes the most common public methods for type conversion mapping:
For a complete description of all available methods for type conversion mapping, see the TopLink JavaDocs.
Object type mappings are instances of the ObjectTypeMapping
class. The following elements are required for an object type mapping:
setAttributeName()
message
setFieldName()
message
addConversionValue()
message
The optional setGetMethodName()
and setSetMethodName()
messages allow TopLink to access the attribute through user-defined methods rather than directly. You do not have to define the accessors when using Java.
The following two methods are useful in a legacy environment, or you want to change the values of the fields:
addToAttributeOnlyConversionValue
(Object fieldValue, Object attributeValue)
- adds conversion value only for the field value to attribute value. This is a one-way mapping from the field to the attribute. This can be used if multiple database values are to mapped to the same object value. When written to the database, the value entered by addConversionValue(Object fieldValue, Object attributeValue)
is used and therefore the original values in the database change.
setDefaultAttributeValue
(Object defaultAttributeValue)
- the default value can be used if the database can store values in addition to those that have been mapped. Any value retrieved from database that is not mapped is substituted for the default value. When writing to the database, the value entered by addConversionValue(Object fieldValue, Object attributeValue)
is used and therefore the original values in the database change.
// Create a new mapping and register it with the descriptor. ObjectTypeMapping typeMapping = new ObjectTypeMapping(); typeMapping.setAttributeName("gender"); typeMapping.setFieldName("GENDER"); typeMapping.addConversionValue("M", "Male"); typeMapping.addConversionValue("F", "Female"); typeMapping.setNullValue("F"); descriptor.addMapping(typeMapping);
Table 7-3 summarizes the most common public methods for object type mapping:
For a complete description of all available methods for object type mapping, see the TopLink JavaDocs.
Element | Default | Method Names |
---|---|---|
Attribute to be mapped * |
not applicable |
setAttributeName(String name) |
Field to be mapped * |
not applicable |
setFieldName(String name) |
* Required property |
Serialized object mappings are instances of the SerializedObjectMapping
class. The following elements are required for a serialized object mapping:
setAttributeName()
message
setFieldName()
message
The optional setGetMethodName()
and setSetMethodName()
messages allow TopLink to access the attribute through user-defined methods rather than directly through the attribute. You do not have to define accessors when using Java 2.
// Create a new mapping and register it with the descriptor.SerializedObjectMapping serializedMapping = new SerializedObjectMapping();
serializedMapping.setAttributeName("jobDescription");
serializedMapping.setFieldName("JOB_DESC");
descriptor.addMapping(serializedMapping);
Table 7-4 summarizes the most common public methods for serialized object mapping:
For a complete description of all available methods for serialized object mapping, see the TopLink JavaDocs.
Element | Default | Method Names |
---|---|---|
Attribute to be mapped * |
not applicable |
setAttributeName(String name) |
Field to be mapped * |
not applicable |
setFieldName(String name) |
* Required property |
Transformation mappings enable you to create specialized translations between how a value is represented in Java and in the database. Use transformation mappings only when mapping multiple fields into a single attribute. Transformation mapping is often appropriate when values from multiple fields are used to create an object.
Transformation mappings are instances of the TransformationMapping
class. The following elements are typically required for a mapping:
setAttributeName()
message; not required for write-only mappings
setAttributeTransformation()
message that expects one or two parameters, a DatabaseRow
and optionally a Session
addFieldTransformation()
message, passing along the database field name and the method name
The optional setGetMethodName()
and setSetMethodName()
messages allow TopLink to access the attribute through user-defined methods rather than directly.
This particular example provides custom support for two of the fields number of fields can be mapped using this approach.
// Create a new mapping and register it with the descriptor. TransformationMapping transformation1 = new TransformationMapping(); transformation1.setAttributeName ("dateAndTimeOfBirth"); transformation1.setAttributeTransformation ("buildDateAndTime"); transformation1.addFieldTransformation("B_DAY", "getDateOfBirth"); transformation1.addFieldTransformation("B_TIME", "getTimeOfBirth"); descriptor.addMapping(transformation1); // Define attribute transformation method to read from the database row public java.util.Date buildDateAndTime(DatabaseRow row) { java.sql.Date sqlDateOfBirth = (java.sql.Date)row.get("B_DAY"); java.sql.Time timeOfBirth = (java.sql.Time)row.get("B_TIME"); java.util.Date utilDateOfBirth = new java.util.Date( sqlDateOfBirth.getYear(), sqlDateOfBirth.getMonth(), sqlDateOfBirth.getDate(), timeOfBirth.getHours(), timeOfBirth.getMinutes(), timeOfBirth.getSeconds()); return utilDateOfBirth; } // Define a field transformation method to write to the database public java.sql.Time getTimeOfBirth() { return new java.sql.Time this.dateAndTimeOfBirth.getHours(), this.dateAndTimeOfBirth.getMinutes(), this.dateAndTimeOfBirth.getSeconds()); } // Define a field transformation method to write to the database public java.sql.Date getDateOfBirth() { return new java.sql.DateOfBirth this.dateAndTimeOfBirth.getYear(), this.dateAndTimeOfBirth.getMonth(), this.dateAndTimeOfBirth.getDate()); }
// Create a new mapping and register it with the descriptor. TransformationMapping transformation2 = new transformation2.setAttributeName("designation"); transformation2.setGetMethodName ("getDesignationHolder"); transformation2.setSetMethodName ("setDesignationHolder"); transformation2.setAttributeTransformation ("getRankFromRow"); transformation2.addFieldTransformation("RANK", "getRankFromObject"); transformation2.useIndirection(); descriptor.addMapping(transformation2);
//Define an attribute transformation method to read from database row.public String getRankFromRow()
{
Integer value = new Integer(((Number)row.get("RANK)).intValue());
String rank = null;
if (value.intValue() == 1) {
rank = "Executive";
}}
if (value.intValue() == 2) {
rank = "Non-Executive";
}
return rank;
}
//Define a field transformation method to write to the database. public Integer getRankFromObject() { Integer rank = null; if (getDesignation().equals("Executive"))
rank = new Integer(1); if (getDesignation().equals("Non-Executive"))
rank = new Integer(2); return rank; } //Provide accessor methods for the indirection. private ValueHolderInterface designation; public ValueHolderInterface getDesignationHolder() { return designation; } public void setDesignationHolder(ValueHolderInterface value) { designation = value;
Table 7-5 summarizes the most common public methods for transformation mapping:
For a complete description of all available methods for transformation mapping, see the TopLink JavaDocs.
Relational mappings define how persistent objects reference other persistent objects. These mappings include all of the following:
Aggregate object mappings are instances of the AggregateObjectMapping
class. This mapping is associated to an attribute in each of the parent classes. The following elements are required for an aggregate object mapping to be viable:
setAttributeName()
message
setReferenceClass()
message
The optional setGetMethodName()
and setSetMethodName()
messages allow TopLink to access the attribute through user-defined methods rather than directly.
By default the mapping allows null references to its target class, so it does not create an instance of the target object. To prevent a parent from having a null reference, send the dontAllowNull()
message, which results in an instance of the child with its attributes set to null.
The following modifications to the target (child) class descriptor are required:
descriptorIsAggregate()
message to the descriptor
// Create a new mapping and register it with the source descriptor. AggregateObjectMapping aggregateMapping = new AggregateObjectMapping(); aggregateMapping.setAttributeName("employPeriod"); aggregateMapping.setReferenceClass(Period.class); descriptor.addMapping(aggregateMapping);
The aggregate target descriptor does not need a mapping to its parent, and does not need any table or primary key information.
// Create a descriptor for the aggregate class. The table name and primary key are not specified in the aggregate descriptor. Descriptor descriptor = new Descriptor(); descriptor.setJavaClass(Period.class); descriptor.descriptorIsAggregate(); // Define the attribute mappings or relationship mappings. descriptor.addDirectMapping("startDate", "START_DATE"); descriptor.addDirectMapping("endDate", "END_DATE"); return descriptor;
The field names must be translated in the Project descriptor as shown in Table 7-1. No changes need to be made to the Period
class descriptor to implement this second parent.
// Create a new mapping and register it with the parent descriptor. AggregateObjectMapping aggregateMapping = new AggregateObjectMapping(); aggregateMapping.setAttributeName("projectPeriod"); aggregateMapping.setReferenceClass(Period.class); aggregateMapping.addFieldNameTranslation("S_DATE", "START_DATE"); aggregateMapping.addFieldNameTranslation("E_DATE", "END_DATE"); descriptor.addMapping(aggregateMapping);
Table 7-6 summarizes the most common public methods for aggregate object mapping:
For a complete description of all available methods for aggregate object mapping, see the TopLink JavaDocs.
Element | Default | Method Names |
---|---|---|
Attribute to be mapped * |
not applicable |
setAttributeName(String name) |
Set parent class * |
not applicable |
setReferenceClass(Class aClass) |
* Required property |
One-to-one mappings, are instances of the OneToOneMapping()
class, that require the following:
setAttributeName()
message
setReferenceClass()
message
setForeignKeyFieldName()
message and passing the foreign key field from the source table that references the primary key of the target table.
If the mapping has a bi-directional relationship where the two classes in the relationship reference each other with one-to-one mappings, then to properly set up the foreign key information:
setForeignKeyFieldName()
message.
setTargetForeignKeyFieldName()
message.
It is also possible to set up composite foreign key information by sending the addForeignKeyFieldName()
and addTargetForeignKeyFieldName()
messages.
// Create a new mapping and register it with the descriptor. OneToOneMapping oneToOneMapping = new OneToOneMapping(); oneToOneMapping.setAttributeName("address"); oneToOneMapping.setReferenceClass(Address.class); oneToOneMapping.setForeignKeyFieldName("ADDRESS_ID"); descriptor.addMapping(oneToOneMapping);
The foreign key is stored in the Policy's table referencing the composite primary key of the Carrier.
// In the Policy class, which will hold the foreign key, create the mapping which references the Carrier class. OneToOneMapping carrierMapping = new OneToOneMapping(); carrierMapping.setAttributeName("carrier"); carrierMapping.setReferenceClass(Carrier.class); carrierMapping.addForeignKeyFieldName("INSURED_ID", "CARRIER_ID"); carrierMapping.addForeignKeyFieldName("INSURED_TYPE", "TYPE"); descriptor.addMapping(carrierMapping);. . . // In the Carrier class, create the mapping which references the Policy class. OneToOneMapping policyMapping = new OneToOneMapping(); policyMapping.setAttributeName("masterPolicy"); policyMapping.setReferenceClass(Policy.class); policyMapping.addTargetForeignKeyFieldName("INSURED_ID", "CARRIER_ID"); policyMapping.addTargetForeignKeyFieldName("INSURED_TYPE", "TYPE"); descriptor.addMapping(policyMapping);
Table 7-7 summarizes the most common public methods for one-to-one mapping:
For a complete description of all available methods for one-to-one mapping, see the TopLink JavaDocs.
Variable one-to-one mappings are instances of the VariableOneToOneMapping()
class. The following elements are required for a variable one-to-one mapping:
setAttributeName()
message
setReferenceClass()
message
setForeignQueryKeyName()
message and passing the source foreign key field name and the target abstract query key name on the interface descriptor.
If the mapping uses a class indicator field:
VariableOneToOneMapping variableOneToOneMapping = new VariableOneToOneMapping(); variableOneToOneMapping.setAttributeName("contact"); variableOneToOneMapping.setReferenceClass (Contact.class); variableOneToOneMapping.setForeignQueryKeyName ("C_ID", "id"); variableOneToOneMapping.setTypeFieldName("TYPE"); variableOneToOneMapping.addClassIndicator(Email.class, "Email"); variableOneToOneMapping.addClassIndicator(Phone.class, "Phone"); variableOneToOneMapping.dontUseIndirection(); variableOneToOneMapping.privateOwnedRelationship();
VariableOneToOneMapping variableOneToOneMapping = new VariableOneToOneMapping(); variableOneToOneMapping.setAttributeName("contact"); variableOneToOneMapping.setReferenceClass (Contact.class); variableOneToOneMapping.setForeignQueryKeyName ("C_ID", "id"); variableOneToOneMapping.dontUseIndirection(); variableOneToOneMapping.privateOwnedRelationship();
Table 7-8 summarizes the most common public methods for variable one-to-one mapping:
For a complete description of all available methods for variable one-to-one mapping, see the TopLink JavaDocs.
Direct collection mappings are instances of the DirectCollectionMapping
class. The following elements are required for a direct collection mapping:
setAttributeName()
message
setReferenceTableName()
message
setDirectFieldName()
message
setReferenceKeyFieldName()
message and passing the name of the field that is a foreign reference to the primary key of the source object.
DirectCollectionMapping directCollectionMapping = new DirectCollectionMapping(); directCollectionMapping.setAttributeName ("responsibilitiesList"); directCollectionMapping.setReferenceTableName ("RESPONS"); directCollectionMapping.setDirectFieldName("DESCRIP"); directCollectionMapping.setReferenceKeyFieldName ("EMP_ID"); directCollectionMapping.useCollectionClass (Vector.class); // the default descriptor.addMapping(directCollectionMapping);
Table 7-9 summarizes the most common public methods for direct collection mapping:
For a complete description of all available methods for direct collection mapping, see the TopLink JavaDocs.
Aggregate collection mappings are used to represent the aggregate relationship between a single-source object and a collection of target objects. Unlike the TopLink one-to-many mappings, in which there should be a one-to-one back reference mapping from the target objects to the source object, there is no back reference required for the aggregate collection mappings because the foreign key relationship is resolved by the aggregation.
To implement an aggregate collection mapping:
Although similar in behavior to 1-many mappings, an aggregate collection is not a replacement for 1-many mappings, and great care should be exercised when choosing an aggregate collection over a one-to-many mapping. aggregate collections should only be used in situations where the target collections are reasonable in size and having a one-to-one mapping from the target to the source is difficult.
Consider using a one-to-many relationship rather than an aggregate collection, because one-to-many relationships offer better performance and are more robust and scalable.
In addition, aggregate collections are privately owned by the source of the relationship and should not be shared or referenced by other objects.
Aggregate collection descriptors can make use of inheritance. The subclasses must also be declared as aggregate collection. The subclasses can have their own mapped tables, or share the table with their parent class.
In a Java Vector, the owner references its parts. In a relational database, the parts reference their owners. Relational databases use this implementation to make querying more efficient.
Aggregate collection mappings are instances of the AggregateCollectionMapping
class. The following elements are required for an aggregate collection mapping:
setAttributeName()
message
setReferenceClass()
message.
addTargetForeignKeyFieldName()
message and passing the field name of the target foreign key and the source the primary key in the source table.
// In the PolicyHolder class, create the mapping which references the Phone
class
AggregateCollectionMapping phonesMapping = new AggregateCollectionMapping();
phonesMapping.setAttributeName("phones");
phonesMapping.setGetMethodName("getPhones");
phonesMapping.setSetMethodName("setPhones");
phonesMapping.setReferenceClass("Phone.class");
phonesMapping.dontUseIndirection();
phonesMapping.privateOwnedRelationship;
phonesMapping.addTargetForeignKeyFieldName ("INS_PHONE.HOLDER_
SSN","HOLDER.SSN");
descriptor.addMapping(phonesMapping);
Table 7-10 summarizes the most common public methods for aggregate collection mapping:
For a complete description of all available methods for aggregate collection mapping, see the TopLink JavaDocs.
Direct map mappings store instances that implement java.util.Map
. Unlike one-to-manys or many to manys, the keys and values of the map in this type of mapping are Java objects that do not have descriptors. The object type stored in the key and the value of direct map are Java primitive wrapper types such as String objects.
Support for primitive data types such as int is not provided since Java maps only hold objects.
Direct map mappings are instances of the DirectMapMapping
class. The following elements are required for a direct map mapping:
setAttributeName()
message
setReferenceTableName()
message
setDirectKeyFieldName()
message
setReferenceKeyFieldName()
message and passing the name of the field that is a foreign reference to the primary key of the source object
setDirectFieldName()
message
setKeyClass()
message
setValueClass()
message
DirectMapMapping directMapMapping = new DirectMapMapping(); directMapMapping.setAttributeName("cities"); directMapMapping.setReferenceTableName("CITY_TEMP"); directMapMapping.setReferenceKeyFieldName("RECORD_ID"); directMapMapping.setDirectKeyFieldName("CITY"); directMapMapping.setDirectFieldName("TEMPERATURE"); directMapMapping.setKeyClass(String.class); directMapMapping.setValueClass(Integer.class); descriptor.addMapping(directMapMapping);
Table 7-11 summarizes the most common public methods for direct map mapping:
For a complete description of all available methods for direct map mapping, see the TopLink JavaDocs.
One-to-many mappings are instances of the OneToManyMapping
class. The following elements are required for a one-to-many mapping:
setAttributeName()
message
setReferenceClass()
message
setTargetForeignKeyFieldName()
message and passing a field in the target object's associated table that refers to the primary key in the owning object's table.
// In the Employee class, create the mapping which references the Phone class. oneToManyMapping = new OneToManyMapping(); oneToManyMapping.setAttributeName("phoneNumbers"); oneToManyMapping.setReferenceClass(PhoneNumber.class); oneToManyMapping.setTargetForeignKeyFieldName("EMPID"); descriptor.addMapping(oneToManyMapping); . . . // In the Phone class, which will hold the foreign key, create the mapping which references the Employee class. OneToOneMapping oneToOneMapping = new OneToOneMapping(); oneToOneMapping.setAttributeName("owner"); oneToOneMapping.setReferenceClass(Employee.class); oneToOneMapping.setForeignKeyFieldName("EMPID"); descriptor.addMapping(oneToOneMapping);
Table 7-12 summarizes the most common public methods for one-to-many mapping:
For a complete description of all available methods for one-to-many mapping, see the TopLink JavaDocs.
Many-to-many mappings are instances of the ManyToManyMapping
class. The following elements are required for a many-to-many mapping:
setAttributeName()
message
setReferenceClass()
message
setRelationTableName()
message
setSourceRelationKeyFieldName()
and setTargetRelationKeyFieldName()
messages; if the source or target primary keys are composite, send the addSourceRelationKeyFieldName()
or addTargetRelationKeyFieldName()
messages
// In the Employee class, create the mapping which references the Project class.
ManyToManyMapping manyToManyMapping = new ManyToManyMapping();
manyToManyMapping.setAttributeName("projects");
manyToManyMapping.setReferenceClass(Project.class);
manyToManyMapping.setRelationTableName("PROJ_EMP");
manyToManyMapping.setSourceRelationKeyFieldName ("EMPID");
manyToManyMapping.setTargetRelationKeyFieldName ("PROJID");
descriptor.addMapping(manyToManyMapping);
Table 7-13 summarizes the most common public methods for many-to-many mapping:
For a complete description of all available methods for many-to-many mapping, see the TopLink JavaDocs.
Relational mappings define how persistent objects reference other persistent objects. Object relational mappings allow for an object model to be persisted into an object-relational data-model. TopLink does not directly support these mappings - they must be defined in code through amendment methods.
TopLink enables you to leverage the following object relational mapping types:
In an object-relational data-model, structures can contain arrays (collections of other data types). These arrays can contain primitive data types or collections of other structures. TopLink stores the arrays with their parent structure in the same table.
TopLink supports arrays of primitive data through the ArrayMapping
. This is similar to DirectCollectionMapping
- it represents a collection of primitives in Java. However, the ArrayMapping
does not require an additional table to store the values in the collection.
Array mappings are instances of the ArrayMapping
class. You must associate this mapping to an attribute in the parent class. TopLink requires the following elements for an array mapping:
setAttributeName( )
message.
setFieldName()
message.
setStructureName()
message.
// Create a new mapping and register it with the source descriptor. ArrayMapping arrayMapping = new ArrayMapping(); arrayMapping.setAttributeName("responsibilities"); arrayMapping.setStructureName("Responsibilities_t"); arrayMapping.setFieldName("RESPONSIBILITIES"); descriptor.addMapping(arrayMapping);
Table 7-14 summarizes the most common public methods for array mapping:
For a complete description of all available methods for array mapping, see the TopLink JavaDocs.
In an object-relational data-model, object arrays allow for an array of object types or structures to be embedded into a single column in a database table or an object table.
Object array mappings are instances of the ObjectArrayMapping
class. You must associate this mapping to an attribute in the parent class. TopLink requires the following elements for an array mapping:
setAttributeName()
message.
setFieldName()
message.
setStructureName ()
message.
Use the optional setGetMethodName()
and setSetMethodName()
messages to access the attribute through user-defined methods rather than directly.
// Create a new mapping and register it with the source descriptor. ObjectArrayMapping phonesMapping = new ObjectArrayMapping(); phonesMapping.setAttributeName("phones"); phonesMapping.setGetMethodName("getPhones"); phonesMapping.setSetMethodName("setPhones"); phonesMapping.setStructureName("PHONELIST_TYPE"); phonesMapping.setReferenceClass(Phone.class); phonesMapping.setFieldName("PHONES"); descriptor.addMapping(phonesMapping);
Table 7-15 summarizes the most common public methods for object array mapping:
For a complete description of all available methods for object array mapping, see the TopLink JavaDocs.
In an object-relational data-model, structures are user defined data-types or object-types. This is similar to a Java class - it denies attributes or fields in which each attribute is either:
TopLink maps each structure to a Java class defined in your object model and defines a descriptor for each class. A StructureMapping
maps nested structures, similar to an AggregateObjectMapping
. However, the structure mapping supports null values and shared aggregates without requiring additional settings (because of the object-relational support of the database).
Structure mappings are instances of the StructureMapping
class. You must associate this mapping to an attribute in each of the parent classes. TopLink requires the following elements for an array mapping:
setAttributeName()
message.
setFieldName()
message.
setReferenceClass()
message.
Use the optional setGetMethodName()
and setSetMethodName()
messages to access the attribute through user-defined methods rather than directly.
You must make the following changes to the target (child) class descriptor:
descriptorIsAggregate()
message to indicate it is not a root level.
// Create a new mapping and register it with the source descriptor. StructureMapping structureMapping = new StructureMapping(); structureMapping.setAttributeName("address"); structureMapping.setReferenceClass(Address.class); structureMapping.setFieldName("address"); descriptor.addMapping(structureMapping);
The aggregate target descriptor does not need a mapping to its parent, or any table or primary key information.
// Create a descriptor for the aggregate class. The table name and primary key are not specified in the aggregate descriptor. ObjectRelationalDescriptor descriptor = new ObjectRelationalDescriptor (); descriptor.setJavaClass(Address.class); descriptor.setStructureName("ADDRESS_T"); descriptor.descriptorIsAggregate(); // Define the field ordering descriptor.addFieldOrdering("STREET"); descriptor.addFieldOrdering("CITY"); ... // Define the attribute mappings or relationship mappings. ...
Table 7-16 summarizes the most common public methods for structure mapping:
For a complete description of all available methods for structure mapping, see the TopLink JavaDocs.
In an object-relational data-model, structures reference each other through refs - not through foreign keys (as in a traditional data-model). Refs are based on the target structure's ObjectID
.
TopLink supports refs through the ReferenceMapping
. They represent an object reference in Java, similar to a OneToOneMapping
. However, the reference mapping does not require foreign key information.
Reference mappings are instances of the ReferenceMapping
class. You must associate this mapping to an attribute in the source class. TopLink requires the following elements for a reference mapping:
setAttributeName( )
message.
setFieldName( )
message.
setRefrenceClass ( )
message.
Use the optional setGetMethodName( )
and setSetMethodName( )
messages to access the attribute through user-defined methods rather than directly.
// Create a new mapping and register it with the source descriptor. ReferenceMapping referenceMapping = new ReferenceMapping(); referenceMapping.setAttributeName("manager"); referenceMapping.setReferenceClass(Employee.class); referenceMapping.setFieldName("MANAGER"); descriptor.addMapping(refrenceMapping);
Table 7-17 summarizes the most common public methods for reference mapping:
For a complete description of all available methods for reference mapping, see the TopLink JavaDocs.
Nested table types model an unordered set of elements. These elements may be built-in or user-defined types. Nested tables typically represent a one-to-many or many-to-many relationship of references to another independent structure. They support querying and joining better than Varrays that are inlined to the parent table.
TopLink supports nested table through the NestedTableMapping
. They represent a collection of object references in Java, similar to a OneToManyMapping
or ManyToManyMapping
. However, the nested table mapping does not require foreign key information (like a one-to-many mapping) or the relational table (like a many-to-many mapping).
Nested table mappings are instances of the NestedTableMapping
class. This mapping is associated to an attribute in the parent class. The following elements are required for a nested table mapping to be viable:
setAttributeName()
message
setFieldName()
message
setStructureName()
message
Use the optional setGetMethodName()
and setSetMethodName()
messages to allow TopLink to access the attribute through user-defined methods rather than directly.
// Create a new mapping and register it with the source descriptor. NestedTableMapping policiesMapping = new NestedTableMapping(); policiesMapping.setAttributeName("policies"); policiesMapping.setGetMethodName("getPolicies"); policiesMapping.setSetMethodName("setPolicies"); policiesMapping.setReferenceClass(Policy.class); policiesMapping.dontUseIndirection(); policiesMapping.setStructureName("POLICIES_TYPE"); policiesMapping.setFieldName("POLICIES"); policiesMapping.privateOwnedRelationship(); policiesMapping.setSelectionSQLString("select p.* from policyHolders ph, table(ph.policies) t, policies p where ph.ssn=#SSN and ref(p) = value(t)"); descriptor.addMapping(policiesMapping);
Table 7-18 summarizes the most common public methods for nested table mapping:
For a complete description of all available methods for nested table mapping, see the TopLink JavaDocs.
|
Copyright © 2002 Oracle Corporation. All Rights Reserved. |
|