Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide 10g (10.1.3.1.0) Part Number B28221-02 |
|
|
View PDF |
OC4J supports the following inheritance strategies for mapping a class or class hierarchy to a relational database schema:
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
.In this strategy, fields that are specific to a subclass are mapped to a different 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".
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".
This section describes the following:
The following examples show how to configure inheritance using a joined subclass approach (see "Joined Subclass"): Example 7-33 shows how to use the @Inheritance
annotation in the base class Project
. Example 7-34 and Example 7-35 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-34, 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-33 @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-34 @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; } ... }
The following examples show how to configure inheritance using a single table for each class hierarchy approach (see "Single Table for Each Class Hierarchy"): Example 7-33 shows how to use the @Inheritance
annotation in the base class Project
. Example 7-34 and Example 7-35 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-36 @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 { ... }