Skip Headers

Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide 10g Release 3 (10.1.3) B14428-01 |
| Home | Solution Area | Contents | Index

Previous | Next |

# Configuring Inheritance for an EJB 3.0 Entity

OC4J supports the following inheritance strategies for mapping a class or class hierarchy to a relational database schema:

- Joined Subclass

- Single Table per Class Hierarchy

You can configure either approach using annotations (see "Using Annotations").

Note: For an EJB 3.0 inheritance code example, see: `http://www.oracle.com/technology/tech/java/oc4j/ejb3/howtos-ejb3/howtoejb30inheritance/doc/how-to-ejb30-inheritance.html`.

## Joined Subclass

In this strategy, fields that are specific to a subclass are mapped to a separate table than the fields that are common to the parent class, and a join is performed to instantiate the subclass.

The root of the class hierarchy is represented by a single table. Each subclass is represented by a separate table that contains the columns that are specific to the subclass (not inherited from its superclass), as well as the column that represent the subclass's primary key. If the subclass does not have any additional state over its superclass, a separate table is not required.

If the subclass table has primary key column, it serves as a foreign key to the primary key of the superclass table. If the subclass table primary key column name is the same as that of the primary key column of the superclass table, OC4J infers this relationship. If the subclass table primary key column name is not the same as that of the primary key column of the superclass table (or, if the subclass table does not have primary key column), you must specify a subclass table column to use to join the primary table of an entity subclass to the primary table of its superclass.

The primary table of the superclass also has a column that serves as a discriminator column, that is, a column whose value identifies the specific subclass to which the instance that is represented by the row belongs.

For more information, see "Configuring Joined Subclass Inheritance with Annotations".

## Single Table per Class Hierarchy

In this strategy, all the classes in a hierarchy are mapped to a single table. The table has a column that serves as a discriminator column. Each subclass that adds additional state maps to this new state only in this single table. Such columns are only used by that subclass.

For more information, see "Configuring Single Table Inheritance with Annotations".

## Using Annotations

This section describes the following:

- Configuring Joined Subclass Inheritance with Annotations

- Configuring Single Table Inheritance with Annotations

### Configuring Joined Subclass Inheritance with Annotations

The following examples show how to configure inheritance using a joined subclass approach (see "Joined Subclass"): Example 7-26 shows how to use the `@Inheritance` annotation in the base class `Project`. Example 7-27 and Example 7-28 show how to use the `@Inheritance` annotation in derived classes `LargeProject` and `SmallProject`, respectively.

The primary table is `EJB_PROJECT` to which both `Project` and `SmallProject` are mapped. `EJB_PROJECT` has a discriminator column called `PROJ_TYPE` that represents `Project`, `LargeProject` and `SmallProject` with values `P`, `L` and `S`, respectively. `LargeProject` adds additional state to `Project`, so is mapped to its own table, `EJB_LPROJECT`, which contains fields specific to `LargeProject`, such as `BUDGET`. Note that EJB_LPROJECT does not have a primary key column; instead it has a foreign key (`PROJ_ID`) that has the same name as the primary key of `EJB_PROJECT`.

Note that in Example 7-27, because the LargeProject class primary key column name (`LARGE_PROJECT_ID`) is not the same as that of the primary key column of the superclass table (`ID`), you must use the `@InheritanceJoinColumn` annotation to specify the column used to join the LargeProject primary table to the primary table of its superclass.

Example 7-26 @Inheritance: Base Class Project in Joined Subclass Inheritance

    @Entity
    @Table(name="EJB_PROJECT")
    @Inheritance(strategy=JOINED, discriminatorValue="P")
    @DiscriminatorColumn(name="PROJ_TYPE")
    public class Project implements Serializable
    {
    ...
        @Id()
        @Column(name="PROJECT_ID", primaryKey=true)
        public Integer getId()
        {
            return id;
        }
    ...
    }

Example 7-27 @Inheritance: Derived Class LargeProject in Joined Subclass Inheritance

    @Entity
    @Table(name="EJB_LPROJECT")
    @Inheritance(discriminatorValue="L")
    @InheritanceJoinColumn(name="LARGE_PROJECT_ID")
    public class LargeProject extends Project
    {
    ...
        @Id()
        @Column(name="LARGE_PROJECT_ID", primaryKey=true)
        public Integer getProjectId()
        {
            return projectId;
        }
    ...
    }

Example 7-28 @Inheritance: Derived Class SmallProject in Joined Subclass Inheritance

    @Entity
    @Table(name="EJB_PROJECT")
    @Inheritance(discriminatorValue="S")
    public class SmallProject extends Project
    {
    ...
    }

### Configuring Single Table Inheritance with Annotations

The following examples show how to configure inheritance using a single table per class hierarchy approach (see "Single Table per Class Hierarchy"): Example 7-26 shows how to use the `@Inheritance` annotation in the base class `Project`. Example 7-27 and Example 7-28 show how the `@Inheritance` annotation is not needed in derived classes `LargeProject` and `SmallProject`, respectively.

The primary table is `EJB_PROJECT` to which both `Project` and `SmallProject` are mapped. The `EJB_PROJECT` table would contain all the columns for `Project` and an additional column (`BUDGET`) used only by `LargeProject`.

Example 7-29 @Inheritance: Base Class Project in Single Table Inheritance

    @Entity
    @Table(name="EJB_PROJECT")
    @Inheritance(strategy=SINGLE_TABLE, discriminatorValue="P")
    @DiscriminatorColumn(name="PROJ_TYPE")
    public class Project implements Serializable
    {
    ...
    }

Example 7-30 @Inheritance: Derived Class LargeProject in Single Table Inheritance

    @Entity
    @Inheritance(discriminatorValue="L")
    public class LargeProject extends Project
    {
    ...
    }

Example 7-31 @Inheritance: Derived Class SmallProject in Single Table Inheritance

    @Entity
    @Inheritance(discriminatorValue="S")
    public class SmallProject extends Project
    {
    ...
    }