8. Heterogeneous Collections


Heterogeneous collections are collections that can hold objects of multiple classes. VBSF allows you to specify heterogeneous collections by providing any of the following:

  1. The name of a superclass

    In this case the collection can hold objects of all subclasses of the supplied superclass. The collection can also hold objects of the superclass itself if the superclass is a non-abstract persistent class that has been mapped to the database.

  2. The name of an interface

    The collection can hold objects of all the classes that implement the supplied interface.

  3. Multiple class names

    In this case the collection can hold objects of all the supplied classes and the classes do not have share a common interface or extend a common base class.

There are no restrictions on how classes that belong to a heterogeneous collection must be mapped to the database. Each class could be mapped to a different database table, or the classes could share a common table by means of filters. However, all objects in a heterogeneous collection must have unique IDs. As a result, you must be careful that object IDs are assigned using a mechanism that generates unique IDs across all classes in a heterogeneous collection, specially if the classes are mapped to more than one table. Global IDs can be generated using methods such as timestamp based database functions, shared sequences (e.g. Oracle), shared sequence (ID) tables (see vposdemo and vshapesdemo applications), or shared ID generator objects (see viddemo sample application). For details on these and other ID generation methods see section 4.4. ID Generation Methods.

A heterogeneous reference occurs when a referenced object can be of multiple types. Heterogeneous ownership occurs when an object can be owned by an object which can be of multiple types. In either case the name of a superclass or the name of an interface must be specified as the referenced class or the owner class.

The vposdemo and vshapesdemo sample applications provide many examples of heterogeneous support. The following sections describe in more detail how heterogeneous collections, heterogeneous references, and heterogeneous ownership are implemented.

 

Defining Heterogeneous Collections

It is only necessary to explicitly define a heterogeneous collection when the classes in the collection are not related by a common superclass or interface. In the case of heterogeneous collections based on a base class or common interface there is NO need to define a heterogeneous collection.

To create a heterogeneous collection click on the "Heterogeneous Collections" folder in the tree, and chose the Object->New menu option. When prompted, enter the name of the collection and click OK. A Heterogeneous Collection object is then created and added under "Heterogeneous Collections" folder.

To edit a heterogeneous collection select it in the tree so its corresponding form will be displayed on the right hand side of the split pane as shown below:

mthcollection.jpg (70578 bytes)

Check the Owned collection option if you are defining an owned heterogeneous collection. Normally owned heterogeneous collections are defined in the Attribute Mapping Form by checking the Heterogeneous option. It is only necessary to define an owned heterogeneous collection using this form if ad-hoc queries without regard for ownership must be performed on the owned collection. See section 4.2.3. Ad-hoc & Constrained Queries of Owned Classes of the Programmer's Guide for details.

To individually specify each of the classes that belong in the collection you must either select the classes from the Classes available listing and click Add to add them to the Classes in collection list, or you must enter each class name in the Superclass, interface, or class name text field and click Add to add each class name to the Classes in collection list.

If the collection is based on a common superclass or interface, you may simply enter the name of the superclass or interface in the Superclass, interface, or class name text field. In this case there is no need to click on the Add button to add the class to the Classes in collection list, as this is done automatically when you leave the form. You can also select the common superclass from the Classes available listing and click Add to add it to the Classes in collection list.

To remove one or more classes from the Classes in collection list simply select the class(es) and click Remove.

 

Next Section 

Return to Table of Contents