Skip Headers

# Using Class Extraction Methods

Previous

Next

You can define a Java method to compute the class indicator based on any available information in the object's data source record. Such a method is called a class extraction method.

Using a class extraction method, you do not need to include an explicit class indicator field in your data model and you can handle relationships that are too complex to describe using class indicator fields.

A class extraction method must have the following characteristics:

- it must be defined on the root descriptor's class

- it must be static

- it must take a `Record` as an argument

- it must return the `java.lang.Class` object to use for the `Record` passed in

You may also need to define only-instances and with-all-subclasses expressions (see "Specifying Expressions for Only-Instances and With-All-Subclasses").

For example, Table: EMPLOYEE Table lists the rows in the `EMPLOYEE` table. The `Employee` class is the base class. `Director`, `Manager`, `Programmer`, and `TechWriter` classes each derive from the `Employee` class. However, in your application, instances of `Manager`, `Programmer`, and `TechWriter` classes must be represented as `Employee` instances and instances of `Director` must be represented as `Director` instances. Because there is no a one-to-one correspondence between class and `JOB_TYPE` field value, the `JOB_TYPE` field alone cannot serve as a class indicator field (see "Using Class Indicator Fields"). To resolve this issue, you could use the class extraction method shown in Example: Class Extraction Method.

EMPLOYEE Table

ID | NAME | JOB_TYPE | JOB_TITLE
--- | --- | --- | ---
732 | Bob Jones | 1 | Manager
733 | Sarah Smith | 3 | Technical Writer
734 | Ben Ng | 2 | Director
735 | Sally Johnson | 3 | Programmer

Class Extraction Method

    ...
    // If the JOB_TYPE field value in record equals 2, return the Director class.
    // Return the Employee class for all other JOB_TYPE field values

    public static Class getClassFromRecord(Record record)
    {
        if (record.get("JOB_TYPE").equals(new Integer(2))
        {
            return Director.class;
        }
        else
        {
            return Employee.class;
        }
    }

When configuring inheritance using a class extraction method, Oracle TopLink does not generate SQL for queries on the root class.