Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/JPA/Inheritance"

m (Annotations)
(Inheritance Examples)
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
[[Category:EclipseLink/Example/JPA|Inheritance]]
 +
 
==How to Define Inheritance==
 
==How to Define Inheritance==
 
This document describes how to define inheritance with EclipseLink JPA. See [[Introduction_to_EclipseLink_JPA_%28ELUG%29#Mapping_Inheritance|Mapping Inheritance]] in the ''[[EclipseLink/UserGuide|EclipseLink User's Guide]]'' for complete information.
 
This document describes how to define inheritance with EclipseLink JPA. See [[Introduction_to_EclipseLink_JPA_%28ELUG%29#Mapping_Inheritance|Mapping Inheritance]] in the ''[[EclipseLink/UserGuide|EclipseLink User's Guide]]'' for complete information.
  
 
__TOC__
 
__TOC__
 
  
  
Line 16: Line 17:
 
The underlying database structure of the single table and joined table inheritance is, however, slightly different.
 
The underlying database structure of the single table and joined table inheritance is, however, slightly different.
  
   
+
==Inheritance Examples==
 +
There are two inheritance example models which you can access from SVN or download the example zip. They model an example insurance claims system.
 +
* The inheritance example model provides an example of the four inheritance types, SINGLETABLE, JOINED, TABLE_PER_CLASS, and @MappedSuperclass.
 +
** [http://wiki.eclipse.org/images/f/ff/Org.eclipse.persistence.example.jpa.inheritance.zip zip] - [http://dev.eclipse.org/svnroot/rt/org.eclipse.persistence/trunk/examples/org.eclipse.persistence.example.jpa.inheritance/ SVN]
 +
* The advanced inheritance example model extends the inheritance model with several advanced inheritance features.
 +
** [http://wiki.eclipse.org/images/1/14/Org.eclipse.persistence.example.jpa.advanced.inheritance.zip zip] - [http://dev.eclipse.org/svnroot/rt/org.eclipse.persistence/trunk/examples/org.eclipse.persistence.example.jpa.advanced.inheritance/ SVN]
 +
 
 
==Single Table Inheritance==
 
==Single Table Inheritance==
 
In the ''single'' table inheritance, the entire class hierarchy is represented by a single table. As the following example shows, the '''Project''' class and the '''LargeProject''' class map to the same PROJECT table:
 
In the ''single'' table inheritance, the entire class hierarchy is represented by a single table. As the following example shows, the '''Project''' class and the '''LargeProject''' class map to the same PROJECT table:
 
  
 
{| border="1" cellpadding="5"
 
{| border="1" cellpadding="5"
Line 37: Line 43:
 
|}
 
|}
  
    public class Project {
+
<source lang="java">
 +
public class Project {
 
   
 
   
          protected BigInteger id;
+
      protected BigInteger id;
          protected String description;
+
      protected String description;
 +
      ...
 +
}
 +
</source>
 +
 
 +
<source lang="java">
 +
public class LargeProject extends Project {
 
    
 
    
          ...
+
      protected BigInteger budget;
 
+
      ...  
    }
+
}
 
+
</source>
    public class LargeProject extends Project {
+
 
+
          protected BigInteger budget;
+
+
          ...  
+
+
    }
+
 
+
  
 
The LargeProject class inherits most of its data from the Project class, with the exception of budget, which the LargeProject adds. The discriminator column (TYPE) is added to the table to distinguish between the stored instances.
 
The LargeProject class inherits most of its data from the Project class, with the exception of budget, which the LargeProject adds. The discriminator column (TYPE) is added to the table to distinguish between the stored instances.
  
 
You can configure the single table inheritance using either annotations or XML.
 
You can configure the single table inheritance using either annotations or XML.
 
 
  
 
===Annotations===
 
===Annotations===
 
The following example shows the mapping of the Project and LargeProject classes to the PROJECT table using annotations:
 
The following example shows the mapping of the Project and LargeProject classes to the PROJECT table using annotations:
  
@Entity
+
<source lang="java">
@Table(name="PROJECT")
+
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
+
@Table(name="PROJECT")
@DiscriminatorColumn(name="TYPE", discriminatorType=DiscriminatorType.STRING,length=20)
+
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorValue("P")
+
@DiscriminatorColumn(name="TYPE", discriminatorType=DiscriminatorType.STRING,length=20)
public class Project {
+
@DiscriminatorValue("P")
 +
public class Project {
 
   
 
   
     @Id protected BigInteger id;
+
     @Id
 +
    protected BigInteger id;
 
     protected String description;
 
     protected String description;
 
     ...
 
     ...
 +
}
 +
</source>
 +
 +
<source lang="java">
 +
@Entity
 +
@DiscriminatorValue("L")
 +
public class LargeProject extends Project {
 
   
 
   
}
 
 
@Entity
 
@DiscriminatorValue("L")
 
public class LargeProject extends Project {
 
 
    @Id
 
 
     protected BigInteger budget;
 
     protected BigInteger budget;
 
     ...
 
     ...
+
}
}
+
</source>
  
 
===XML===
 
===XML===
 
You can also map the Project and LargeProject classes to the PROJECT table using XML, as the following example demonstrates:
 
You can also map the Project and LargeProject classes to the PROJECT table using XML, as the following example demonstrates:
  
    <entity name="Project" class="Project" access="FIELD">
+
<source lang="xml">
+
<entity name="Project" class="Project" access="FIELD">
          <table name="PROJECT"/>
+
    <table name="PROJECT"/>
          <inheritance strategy="SINGLE_TABLE"/>
+
    <inheritance strategy="SINGLE_TABLE"/>
          <discriminator-value>P</discriminator-value>
+
    <discriminator-value>P</discriminator-value>
          <discriminator-column name="TYPE"/>
+
    <discriminator-column name="TYPE"/>
          <attributes>
+
    <attributes>
                <id name="id"><column name="ID"/></id>
+
        <id name="id"><column name="ID"/></id>
          </attributes>
+
    </attributes>
+
</entity>
    </entity>
+
<entity name="LargeProject" class="LargeProject" access="FIELD">
+
      <discriminator-value>L</discriminator-value>
    <entity name="LargeProject" class="LargeProject" access="FIELD">
+
</entity>
+
</source>
          <discriminator-value>L</discriminator-value>
+
+
    </entity>
+
 
+
 
+
  
 
==Joined Table Inheritance==
 
==Joined Table Inheritance==
 
In the ''joined'' table inheritance, each class shares data from the root table. In addition, each subclass defines its own table that adds its extended state. The following example shows two tables, PROJECT and L_PROJECT, as well as two classes, Project and LargeProject:
 
In the ''joined'' table inheritance, each class shares data from the root table. In addition, each subclass defines its own table that adds its extended state. The following example shows two tables, PROJECT and L_PROJECT, as well as two classes, Project and LargeProject:
 
  
 
{| border="1" cellpadding="5"
 
{| border="1" cellpadding="5"
Line 127: Line 126:
 
| large project
 
| large project
 
|}
 
|}
 
  
 
{| border="1" cellpadding="5"
 
{| border="1" cellpadding="5"
Line 138: Line 136:
 
|}
 
|}
  
 +
<source lang="java">
 +
public class Project {
  
    public class Project {
+
      protected BigInteger id;
 +
      protected String description;
 +
      ...
 +
}
 +
</source>
 +
 
 +
<source lang="java">
 +
public class LargeProject extends Project {
 
   
 
   
          protected BigInteger id;
+
      protected BigInteger budget;
          protected String description;
+
      ...  
+
}
          ...  
+
</source>
+
    }
+
+
    public class LargeProject extends Project {
+
+
          protected BigInteger budget;
+
+
          ...
+
+
    }
+
  
 
Similar to the example of the single table inheritance, the preceding joined table example demonstrates how the shared data is stored in a single table. The newly defined fields, however, are stored in a separate table.
 
Similar to the example of the single table inheritance, the preceding joined table example demonstrates how the shared data is stored in a single table. The newly defined fields, however, are stored in a separate table.
  
 
You can configure the joined table inheritance using either annotations or XML.
 
You can configure the joined table inheritance using either annotations or XML.
 
 
  
 
===Annotations===
 
===Annotations===
 
The following example shows the mapping of the Project and LargeProject classes to the PROJECT and L_PROJECT tables using annotations:
 
The following example shows the mapping of the Project and LargeProject classes to the PROJECT and L_PROJECT tables using annotations:
  
@Entity
+
<source lang="java">
@Table(name="PROJECT")
+
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
+
@Table(name="PROJECT")
@DiscriminatorColumn(name="TYPE", discriminatorType=DiscriminatorType.STRING,length=20)
+
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorValue("P")
+
@DiscriminatorColumn(name="TYPE", discriminatorType=DiscriminatorType.STRING,length=20)
public class Project {
+
@DiscriminatorValue("P")
 +
public class Project {
 
   
 
   
      @Id protected BigInteger id;
+
    @Id
      protected String description;
+
    protected BigInteger id;
      ...  
+
    protected String description;
 +
    ...  
 +
}
 +
</source>
 +
 
 +
<source lang="java">
 +
@Entity
 +
@Table(name="L_PROJECT")
 +
@DiscriminatorValue("L")
 +
public class LargeProject extends Project {
 
   
 
   
}
+
    protected BigInteger budget;
+
    ...  
@Entity
+
}
@Table(name="L_PROJECT")
+
</source>
@DiscriminatorValue("L")
+
public class LargeProject extends Project {
+
+
      protected BigInteger budget;
+
      ...  
+
+
}
+
  
 
===XML===
 
===XML===
 
You can also map the Project and LargeProject classes to the PROJECT and L_PROJECT tables using XML, as the following example shows:
 
You can also map the Project and LargeProject classes to the PROJECT and L_PROJECT tables using XML, as the following example shows:
  
    <entity name="Project" class="Project" access="FIELD">
+
<source lang="xml">
+
<entity name="Project" class="Project" access="FIELD">
          <table name="PROJECT"/>
+
    <table name="PROJECT"/>
          <inheritance strategy="JOINED"/>
+
    <inheritance strategy="JOINED"/>
          <discriminator-value>P</discriminator-value>
+
    <discriminator-value>P</discriminator-value>
          <discriminator-column name="TYPE"/>
+
    <discriminator-column name="TYPE"/>
          <attributes>
+
    <attributes>
                <id name="id"><column name="ID"/> </id>
+
      <id name="id"><column name="ID"/> </id>
          </attributes>
+
    </attributes>
+
</entity>
    </entity>
+
<entity name="LargeProject" class="LargeProject" access="FIELD">
+
    <table name="L_PROJECT"/>
    <entity name="LargeProject" class="LargeProject" access="FIELD">
+
    <discriminator-value>L</discriminator-value>
+
</entity>
          <table name="L_PROJECT"/>
+
</source>
          <discriminator-value>L</discriminator-value>
+
+
    </entity>
+
 
+
  
 
==Summary==
 
==Summary==
 
 
This document demonstrated the ways to define inheritance for entities using annotations and XML.
 
This document demonstrated the ways to define inheritance for entities using annotations and XML.

Latest revision as of 11:41, 8 December 2009


How to Define Inheritance

This document describes how to define inheritance with EclipseLink JPA. See Mapping Inheritance in the EclipseLink User's Guide for complete information.


Introduction

In JPA, an entity class may inherit from another entity class its behavior and state. The behavior and state become shared between entity classes enabling the inheritance of existing mappings -- you would only need to define mappings for a new state.

There are several types of inheritance. Two of these types -- single table inheritance and joined table inheritance -- have many similarities:

  • Their object model is the same.
  • They can be configured using either annotations or XML.
  • A discriminator column (a single column used for distinguishing to which class type a database row belongs) is required on the database.

The underlying database structure of the single table and joined table inheritance is, however, slightly different.

Inheritance Examples

There are two inheritance example models which you can access from SVN or download the example zip. They model an example insurance claims system.

  • The inheritance example model provides an example of the four inheritance types, SINGLETABLE, JOINED, TABLE_PER_CLASS, and @MappedSuperclass.
  • The advanced inheritance example model extends the inheritance model with several advanced inheritance features.

Single Table Inheritance

In the single table inheritance, the entire class hierarchy is represented by a single table. As the following example shows, the Project class and the LargeProject class map to the same PROJECT table:

PROJECT table
ID TYPE DESCRIPTION BUDGET
1 P small project
2 L large project 1000000
public class Project {
 
      protected BigInteger id;
      protected String description;
      ... 
}
public class LargeProject extends Project {
 
      protected BigInteger budget;
      ... 
}

The LargeProject class inherits most of its data from the Project class, with the exception of budget, which the LargeProject adds. The discriminator column (TYPE) is added to the table to distinguish between the stored instances.

You can configure the single table inheritance using either annotations or XML.

Annotations

The following example shows the mapping of the Project and LargeProject classes to the PROJECT table using annotations:

@Entity
@Table(name="PROJECT")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="TYPE", discriminatorType=DiscriminatorType.STRING,length=20)
@DiscriminatorValue("P")
public class Project {
 
    @Id
    protected BigInteger id;
    protected String description;
    ...
}
@Entity
@DiscriminatorValue("L")
public class LargeProject extends Project {
 
    protected BigInteger budget;
    ...
}

XML

You can also map the Project and LargeProject classes to the PROJECT table using XML, as the following example demonstrates:

<entity name="Project" class="Project" access="FIELD">
    <table name="PROJECT"/>
    <inheritance strategy="SINGLE_TABLE"/>
    <discriminator-value>P</discriminator-value>
    <discriminator-column name="TYPE"/>
    <attributes>
        <id name="id"><column name="ID"/></id>
    </attributes>
</entity>
<entity name="LargeProject" class="LargeProject" access="FIELD">
      <discriminator-value>L</discriminator-value>
</entity>

Joined Table Inheritance

In the joined table inheritance, each class shares data from the root table. In addition, each subclass defines its own table that adds its extended state. The following example shows two tables, PROJECT and L_PROJECT, as well as two classes, Project and LargeProject:

PROJECT table
ID TYPE DESCRIPTION
1 P small project
2 L large project
L PROJECT table
ID BUDGET
2 1000000
public class Project {
 
      protected BigInteger id;
      protected String description;
      ... 
}
public class LargeProject extends Project {
 
      protected BigInteger budget;
      ... 
}

Similar to the example of the single table inheritance, the preceding joined table example demonstrates how the shared data is stored in a single table. The newly defined fields, however, are stored in a separate table.

You can configure the joined table inheritance using either annotations or XML.

Annotations

The following example shows the mapping of the Project and LargeProject classes to the PROJECT and L_PROJECT tables using annotations:

@Entity
@Table(name="PROJECT")
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="TYPE", discriminatorType=DiscriminatorType.STRING,length=20)
@DiscriminatorValue("P")
public class Project {
 
    @Id
    protected BigInteger id;
    protected String description;
    ... 
}
@Entity
@Table(name="L_PROJECT")
@DiscriminatorValue("L")
public class LargeProject extends Project {
 
    protected BigInteger budget;
    ... 
}

XML

You can also map the Project and LargeProject classes to the PROJECT and L_PROJECT tables using XML, as the following example shows:

<entity name="Project" class="Project" access="FIELD">
    <table name="PROJECT"/>
    <inheritance strategy="JOINED"/>
    <discriminator-value>P</discriminator-value>
    <discriminator-column name="TYPE"/>
    <attributes>
       <id name="id"><column name="ID"/> </id>
    </attributes>
</entity>
<entity name="LargeProject" class="LargeProject" access="FIELD">
    <table name="L_PROJECT"/>
    <discriminator-value>L</discriminator-value>
</entity>

Summary

This document demonstrated the ways to define inheritance for entities using annotations and XML.

Back to the top