| <html><head> |
| <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
| <title>6. Inheritance</title><base href="display"><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1"><link rel="home" href="manual.html" title="Apache OpenJPA 2.2 User's Guide"><link rel="up" href="jpa_overview_mapping.html" title="Chapter 13. Mapping Metadata"><link rel="prev" href="jpa_overview_mapping_sequence.html" title="5. Generators"><link rel="next" href="jpa_overview_mapping_discrim.html" title="7. Discriminator"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">6. |
| Inheritance |
| </th></tr><tr><td width="20%" align="left"><a accesskey="p" href="jpa_overview_mapping_sequence.html">Prev</a> </td><th width="60%" align="center">Chapter 13. |
| Mapping Metadata |
| </th><td width="20%" align="right"> <a accesskey="n" href="jpa_overview_mapping_discrim.html">Next</a></td></tr></table><hr></div><div class="section" title="6. Inheritance"><div class="titlepage"><div><div><h2 class="title" style="clear: both" id="jpa_overview_mapping_inher">6. |
| Inheritance |
| </h2></div></div></div><div class="toc"><dl><dt><span class="section"><a href="jpa_overview_mapping_inher.html#jpa_overview_mapping_inher_single">6.1. |
| Single Table |
| </a></span></dt><dd><dl><dt><span class="section"><a href="jpa_overview_mapping_inher.html#jpa_overview_mapping_inher_single_adv">6.1.1. |
| Advantages |
| </a></span></dt><dt><span class="section"><a href="jpa_overview_mapping_inher.html#jpa_overview_mapping_inher_single_disadv">6.1.2. |
| Disadvantages |
| </a></span></dt></dl></dd><dt><span class="section"><a href="jpa_overview_mapping_inher.html#jpa_overview_mapping_inher_joined">6.2. |
| Joined |
| </a></span></dt><dd><dl><dt><span class="section"><a href="jpa_overview_mapping_inher.html#jpa_overview_mapping_inher_joined_adv">6.2.1. |
| Advantages |
| </a></span></dt><dt><span class="section"><a href="jpa_overview_mapping_inher.html#jpa_overview_mapping_inher_joined_disadv">6.2.2. |
| Disadvantages |
| </a></span></dt></dl></dd><dt><span class="section"><a href="jpa_overview_mapping_inher.html#jpa_overview_mapping_inher_tpc">6.3. |
| Table Per Class |
| </a></span></dt><dd><dl><dt><span class="section"><a href="jpa_overview_mapping_inher.html#jpa_overview_mapping_inher_tpc_adv">6.3.1. |
| Advantages |
| </a></span></dt><dt><span class="section"><a href="jpa_overview_mapping_inher.html#jpa_overview_mapping_inher_tpc_disadv">6.3.2. |
| Disadvantages |
| </a></span></dt></dl></dd><dt><span class="section"><a href="jpa_overview_mapping_inher.html#jpa_overview_mapping_inher_together">6.4. |
| Putting it All Together |
| </a></span></dt></dl></div> |
| |
| <a class="indexterm" name="d5e5862"></a> |
| <a class="indexterm" name="d5e5866"></a> |
| <a class="indexterm" name="d5e5869"></a> |
| <a class="indexterm" name="d5e5873"></a> |
| <p> |
| In the 1990's programmers coined the term <span class="emphasis"><em>impedance mismatch |
| </em></span> to describe the difficulties in bridging the object and relational |
| worlds. Perhaps no feature of object modeling highlights the impedance mismatch |
| better than inheritance. There is no natural, efficient way to represent an |
| inheritance relationship in a relational database. |
| </p> |
| <p> |
| <a class="indexterm" name="d5e5878"></a> |
| Luckily, JPA gives you a choice of inheritance strategies, making |
| the best of a bad situation. The base entity class defines the inheritance |
| strategy for the hierarchy with the <code class="classname">Inheritance</code> |
| annotation. <code class="classname">Inheritance</code> has the following properties: |
| </p> |
| <div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"> |
| <p> |
| <code class="literal">InheritanceType strategy</code>: Enum value declaring the |
| inheritance strategy for the hierarchy. Defaults to <code class="literal"> |
| InheritanceType.SINGLE_TABLE</code>. We detail each of the available |
| strategies below. |
| </p> |
| </li></ul></div> |
| <p> |
| The corresponding XML element is <code class="literal">inheritance</code>, which has a |
| single attribute: |
| </p> |
| <div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"> |
| <p> |
| <code class="literal">strategy</code>: One of <code class="literal">SINGLE_TABLE</code>, <code class="literal"> |
| JOINED</code>, or <code class="literal">TABLE_PER_CLASS</code>. |
| </p> |
| </li></ul></div> |
| <p> |
| The following sections describe JPA's standard inheritance strategies. |
| </p> |
| <div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3> |
| <p> |
| OpenJPA allows you to vary your inheritance strategy for each class, rather than |
| forcing a single strategy per inheritance hierarchy. See |
| <a class="xref" href="ref_guide_mapping_jpa.html" title="7. Additional JPA Mappings">Section 7, “ |
| Additional JPA Mappings |
| ”</a> in the Reference Guide for |
| details. |
| </p> |
| </div> |
| <div class="section" title="6.1. Single Table"><div class="titlepage"><div><div><h3 class="title" id="jpa_overview_mapping_inher_single">6.1. |
| Single Table |
| </h3></div></div></div><div class="toc"><dl><dt><span class="section"><a href="jpa_overview_mapping_inher.html#jpa_overview_mapping_inher_single_adv">6.1.1. |
| Advantages |
| </a></span></dt><dt><span class="section"><a href="jpa_overview_mapping_inher.html#jpa_overview_mapping_inher_single_disadv">6.1.2. |
| Disadvantages |
| </a></span></dt></dl></div> |
| |
| <a class="indexterm" name="d5e5904"></a> |
| <a class="indexterm" name="d5e5908"></a> |
| <p> |
| The <code class="literal">InheritanceType.SINGLE_TABLE</code> strategy maps all classes in |
| the hierarchy to the base class' table. |
| </p> |
| <div class="mediaobject"><table border="0" summary="manufactured viewport for HTML img" cellspacing="0" cellpadding="0" width="177"><tr><td><img src="img/inher-superclass-table.png"></td></tr></table></div> |
| <p> |
| In our model, <code class="classname">Subscription</code> is mapped to the <code class="literal"> |
| CNTRCT.SUB</code> table. <code class="classname"> LifetimeSubscription</code>, which |
| extends <code class="classname"> Subscription</code>, adds its field data to this table |
| as well. |
| </p> |
| <div class="example"><a name="jpa_overview_mapping_inher_singleex"></a><p class="title"><b>Example 13.5. |
| Single Table Mapping |
| </b></p><div class="example-contents"> |
| |
| <pre class="programlisting"> |
| @Entity |
| @Table(name="SUB", schema="CNTRCT") |
| @Inheritance(strategy=InheritanceType.SINGLE_TABLE) |
| public class Subscription { |
| ... |
| } |
| |
| @Entity(name="Lifetime") |
| public class LifetimeSubscription |
| extends Subscription { |
| ... |
| } |
| </pre> |
| <p> |
| The same metadata expressed in XML form: |
| </p> |
| <pre class="programlisting"> |
| <entity class="org.mag.subcribe.Subscription"> |
| <table name="SUB" schema="CNTRCT"/> |
| <inheritance strategy="SINGLE_TABLE"/> |
| ... |
| </entity> |
| <entity class="org.mag.subscribe.LifetimeSubscription"> |
| ... |
| </entity> |
| </pre> |
| </div></div><br class="example-break"> |
| <p> |
| Single table inheritance is the default strategy. Thus, we could omit the |
| <code class="literal">@Inheritance</code> annotation in the example above and get the same |
| result. |
| </p> |
| <div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3> |
| <p> |
| <a class="indexterm" name="d5e5930"></a> |
| <a class="indexterm" name="d5e5933"></a> |
| Mapping subclass state to the superclass table is often called <span class="emphasis"><em>flat |
| </em></span> inheritance mapping. |
| </p> |
| </div> |
| <div class="section" title="6.1.1. Advantages"><div class="titlepage"><div><div><h4 class="title" id="jpa_overview_mapping_inher_single_adv">6.1.1. |
| Advantages |
| </h4></div></div></div> |
| |
| <a class="indexterm" name="d5e5939"></a> |
| <p> |
| Single table inheritance mapping is the fastest of all inheritance models, since |
| it never requires a join to retrieve a persistent instance from the database. |
| Similarly, persisting or updating a persistent instance requires only a single |
| <code class="literal">INSERT</code> or <code class="literal">UPDATE</code> statement. Finally, |
| relations to any class within a single table inheritance hierarchy are just as |
| efficient as relations to a base class. |
| </p> |
| </div> |
| <div class="section" title="6.1.2. Disadvantages"><div class="titlepage"><div><div><h4 class="title" id="jpa_overview_mapping_inher_single_disadv">6.1.2. |
| Disadvantages |
| </h4></div></div></div> |
| |
| <a class="indexterm" name="d5e5948"></a> |
| <p> |
| The larger the inheritance model gets, the "wider" the mapped table gets, in |
| that for every field in the entire inheritance hierarchy, a column must exist in |
| the mapped table. This may have undesirable consequence on the database size, |
| since a wide or deep inheritance hierarchy will result in tables with many |
| mostly-empty columns. |
| </p> |
| </div> |
| </div> |
| <div class="section" title="6.2. Joined"><div class="titlepage"><div><div><h3 class="title" id="jpa_overview_mapping_inher_joined">6.2. |
| Joined |
| </h3></div></div></div><div class="toc"><dl><dt><span class="section"><a href="jpa_overview_mapping_inher.html#jpa_overview_mapping_inher_joined_adv">6.2.1. |
| Advantages |
| </a></span></dt><dt><span class="section"><a href="jpa_overview_mapping_inher.html#jpa_overview_mapping_inher_joined_disadv">6.2.2. |
| Disadvantages |
| </a></span></dt></dl></div> |
| |
| <a class="indexterm" name="d5e5955"></a> |
| <a class="indexterm" name="d5e5959"></a> |
| <p> |
| The <code class="literal">InheritanceType.JOINED</code> strategy uses a different table |
| for each class in the hierarchy. Each table only includes state declared in its |
| class. Thus to load a subclass instance, the JPA implementation must |
| read from the subclass table as well as the table of each ancestor class, up to |
| the base entity class. |
| </p> |
| <div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3> |
| <p> |
| <a class="indexterm" name="d5e5966"></a> |
| <a class="indexterm" name="d5e5969"></a> |
| Using joined subclass tables is also called <span class="emphasis"><em>vertical</em></span> |
| inheritance mapping. |
| </p> |
| </div> |
| <div class="mediaobject"><table border="0" summary="manufactured viewport for HTML img" cellspacing="0" cellpadding="0" width="171"><tr><td><img src="img/jpa-inher-joined.png"></td></tr></table></div> |
| <p> |
| <code class="classname">PrimaryKeyJoinColumn</code> annotations tell the JPA |
| implementation how to join each subclass table record to the corresponding |
| record in its direct superclass table. In our model, the <code class="literal">LINE_ITEM.ID |
| </code> column joins to the <code class="literal">CONTRACT.ID</code> column. The |
| <code class="classname">PrimaryKeyJoinColumn</code> annotation has the following |
| properties: |
| </p> |
| <div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"> |
| <p> |
| <code class="literal">String name</code>: The name of the subclass table column. When |
| there is a single identity field, defaults to that field's column name. |
| </p> |
| </li><li class="listitem"> |
| <p> |
| <code class="literal">String referencedColumnName</code>: The name of the superclass |
| table column this subclass table column joins to. When there is a single |
| identity field, defaults to that field's column name. |
| </p> |
| </li><li class="listitem"> |
| <p> |
| <code class="literal">String columnDefinition</code>: This property has the same meaning |
| as the <code class="literal">columnDefinition</code> property on the <code class="classname">Column |
| </code> annotation, described in |
| <a class="xref" href="jpa_overview_mapping_column.html" title="3. Column">Section 3, “ |
| Column |
| ”</a>. |
| </p> |
| </li></ul></div> |
| <p> |
| The XML equivalent is the <code class="literal">primary-key-join-column</code> element. |
| Its attributes mirror the annotation properties described above: |
| </p> |
| <div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"> |
| <p> |
| <code class="literal">name</code> |
| </p> |
| </li><li class="listitem"> |
| <p> |
| <code class="literal">referenced-column-name</code> |
| </p> |
| </li><li class="listitem"> |
| <p> |
| <code class="literal">column-definition</code> |
| </p> |
| </li></ul></div> |
| <p> |
| The example below shows how we use <code class="literal">InheritanceTable.JOINED</code> |
| and a primary key join column to map our sample model according to the diagram |
| above. Note that a primary key join column is not strictly needed, because there |
| is only one identity column, and the subclass table column has the same name as |
| the superclass table column. In this situation, the defaults suffice. However, |
| we include the primary key join column for illustrative purposes. |
| </p> |
| <div class="example"><a name="jpa_overview_mapping_inher_joinedex"></a><p class="title"><b>Example 13.6. |
| Joined Subclass Tables |
| </b></p><div class="example-contents"> |
| |
| <pre class="programlisting"> |
| @Entity |
| @Table(schema="CNTRCT") |
| @Inheritance(strategy=InheritanceType.JOINED) |
| public class Contract |
| extends Document { |
| ... |
| } |
| |
| public class Subscription { |
| ... |
| |
| @Entity |
| @Table(name="LINE_ITEM", schema="CNTRCT") |
| @PrimaryKeyJoinColumn(name="ID", referencedColumnName="ID") |
| public static class LineItem |
| extends Contract { |
| ... |
| } |
| } |
| </pre> |
| <p> |
| The same metadata expressed in XML form: |
| </p> |
| <pre class="programlisting"> |
| <entity class="org.mag.subcribe.Contract"> |
| <table schema="CNTRCT"/> |
| <inheritance strategy="JOINED"/> |
| ... |
| </entity> |
| <entity class="org.mag.subscribe.Subscription.LineItem"> |
| <table name="LINE_ITEM" schema="CNTRCT"/> |
| <primary-key-join-column name="ID" referenced-column-name="PK"/> |
| ... |
| </entity> |
| </pre> |
| </div></div><br class="example-break"> |
| <p> |
| When there are multiple identity columns, you must define multiple <code class="classname"> |
| PrimaryKeyJoinColumn</code>s using the aptly-named <code class="classname"> |
| PrimaryKeyJoinColumns</code> annotation. This annotation's value is an |
| array of <code class="classname"> PrimaryKeyJoinColumn</code> s. We could rewrite |
| <code class="classname">LineItem</code>'s mapping as: |
| </p> |
| <pre class="programlisting"> |
| @Entity |
| @Table(name="LINE_ITEM", schema="CNTRCT") |
| @PrimaryKeyJoinColumns({ |
| @PrimaryKeyJoinColumn(name="ID", referencedColumnName="ID") |
| }) |
| public static class LineItem |
| extends Contract { |
| ... |
| } |
| </pre> |
| <p> |
| In XML, simply list as many <code class="literal"> primary-key-join-column</code> elements |
| as necessary. |
| </p> |
| <div class="section" title="6.2.1. Advantages"><div class="titlepage"><div><div><h4 class="title" id="jpa_overview_mapping_inher_joined_adv">6.2.1. |
| Advantages |
| </h4></div></div></div> |
| |
| <a class="indexterm" name="d5e6023"></a> |
| <p> |
| The joined strategy has the following advantages: |
| </p> |
| <div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"> |
| <p> |
| <a class="indexterm" name="d5e6031"></a> |
| Using joined subclass tables results in the most <span class="emphasis"><em>normalized</em></span> |
| database schema, meaning the schema with the least spurious or redundant data. |
| </p> |
| </li><li class="listitem"> |
| <p> |
| As more subclasses are added to the data model over time, the only schema |
| modification that needs to be made is the addition of corresponding subclass |
| tables in the database (rather than having to change the structure of existing |
| tables). |
| </p> |
| </li><li class="listitem"> |
| <p> |
| Relations to a base class using this strategy can be loaded through standard |
| joins and can use standard foreign keys, as opposed to the machinations required |
| to load polymorphic relations to table-per-class base types, described below. |
| </p> |
| </li></ol></div> |
| </div> |
| <div class="section" title="6.2.2. Disadvantages"><div class="titlepage"><div><div><h4 class="title" id="jpa_overview_mapping_inher_joined_disadv">6.2.2. |
| Disadvantages |
| </h4></div></div></div> |
| |
| <a class="indexterm" name="d5e6040"></a> |
| <p> |
| Aside from certain uses of the table-per-class strategy described below, the |
| joined strategy is often the slowest of the inheritance models. Retrieving any |
| subclass requires one or more database joins, and storing subclasses requires |
| multiple <code class="literal">INSERT</code> or <code class="literal">UPDATE</code> statements. This |
| is only the case when persistence operations are performed on subclasses; if |
| most operations are performed on the least-derived persistent superclass, then |
| this mapping is very fast. |
| </p> |
| <div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3> |
| <p> |
| When executing a select against a hierarchy that uses joined subclass table |
| inheritance, you must consider how to load subclass state. |
| <a class="xref" href="ref_guide_perfpack_eager.html" title="8. Eager Fetching">Section 8, “ |
| Eager Fetching |
| ”</a> in the Reference Guide |
| describes OpenJPA's options for efficient data loading. |
| </p> |
| </div> |
| </div> |
| </div> |
| <div class="section" title="6.3. Table Per Class"><div class="titlepage"><div><div><h3 class="title" id="jpa_overview_mapping_inher_tpc">6.3. |
| Table Per Class |
| </h3></div></div></div><div class="toc"><dl><dt><span class="section"><a href="jpa_overview_mapping_inher.html#jpa_overview_mapping_inher_tpc_adv">6.3.1. |
| Advantages |
| </a></span></dt><dt><span class="section"><a href="jpa_overview_mapping_inher.html#jpa_overview_mapping_inher_tpc_disadv">6.3.2. |
| Disadvantages |
| </a></span></dt></dl></div> |
| |
| <a class="indexterm" name="d5e6052"></a> |
| <a class="indexterm" name="d5e6056"></a> |
| <p> |
| Like the <code class="literal">JOINED</code> strategy, the <code class="literal"> |
| InheritanceType.TABLE_PER_CLASS</code> strategy uses a different table for |
| each class in the hierarchy. Unlike the <code class="literal">JOINED</code> strategy, |
| however, each table includes all state for an instance of the corresponding |
| class. Thus to load a subclass instance, the JPA implementation must |
| only read from the subclass table; it does not need to join to superclass |
| tables. |
| </p> |
| <div class="mediaobject"><table border="0" summary="manufactured viewport for HTML img" cellspacing="0" cellpadding="0" width="189"><tr><td><img src="img/inher-tpc.png"></td></tr></table></div> |
| <p> |
| Suppose that our sample model's <code class="classname">Magazine</code> class has a |
| subclass <code class="classname">Tabloid</code>. The classes are mapped using the |
| table-per-class strategy, as in the diagram above. In a table-per-class mapping, |
| <code class="classname"> Magazine</code>'s table <code class="literal">MAG</code> contains all |
| state declared in the base <code class="classname">Magazine</code> class. <code class="classname"> |
| Tabloid</code> maps to a separate table, <code class="literal"> TABLOID</code>. This |
| table contains not only the state declared in the <code class="classname">Tabloid</code> |
| subclass, but all the base class state from <code class="classname">Magazine</code> as |
| well. Thus the <code class="literal">TABLOID</code> table would contain columns for |
| <code class="literal">isbn</code>, <code class="literal">title</code>, and other <code class="classname"> |
| Magazine</code> fields. These columns would default to the names used in |
| <code class="classname">Magazine</code>'s mapping metadata. |
| <a class="xref" href="jpa_overview_mapping_field.html#jpa_overview_mapping_embed" title="8.3. Embedded Mapping">Section 8.3, “ |
| Embedded Mapping |
| ”</a> will show you how to use |
| <code class="literal">AttributeOverride</code>s and <code class="literal">AssociationOverride</code> |
| s to override superclass field mappings. |
| </p> |
| <div class="example"><a name="jpa_overview_mapping_inher_tpcex"></a><p class="title"><b>Example 13.7. |
| Table Per Class Mapping |
| </b></p><div class="example-contents"> |
| |
| <pre class="programlisting"> |
| @Entity |
| @Table(name="MAG") |
| @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) |
| public class Magazine { |
| ... |
| } |
| |
| @Entity |
| @Table(name="TABLOID") |
| public class Tabloid |
| extends Magazine { |
| ... |
| } |
| </pre> |
| <p> |
| And the same classes in XML: |
| </p> |
| <pre class="programlisting"> |
| <entity class="org.mag.Magazine"> |
| <table name="MAG"/> |
| <inheritance strategy="TABLE_PER_CLASS"/> |
| ... |
| </entity> |
| <entity class="org.mag.Tabloid"> |
| <table name="TABLOID"/> |
| ... |
| </entity> |
| </pre> |
| </div></div><br class="example-break"> |
| <div class="section" title="6.3.1. Advantages"><div class="titlepage"><div><div><h4 class="title" id="jpa_overview_mapping_inher_tpc_adv">6.3.1. |
| Advantages |
| </h4></div></div></div> |
| |
| <a class="indexterm" name="d5e6091"></a> |
| <p> |
| The table-per-class strategy is very efficient when operating on instances of a |
| known class. Under these conditions, the strategy never requires joining to |
| superclass or subclass tables. Reads, joins, inserts, updates, and deletes are |
| all efficient in the absence of polymorphic behavior. Also, as in the joined |
| strategy, adding additional classes to the hierarchy does not require modifying |
| existing class tables. |
| </p> |
| </div> |
| <div class="section" title="6.3.2. Disadvantages"><div class="titlepage"><div><div><h4 class="title" id="jpa_overview_mapping_inher_tpc_disadv">6.3.2. |
| Disadvantages |
| </h4></div></div></div> |
| |
| <a class="indexterm" name="d5e6098"></a> |
| <p> |
| Polymorphic relations to non-leaf classes in a table-per-class hierarchy have |
| many limitations. When the concrete subclass is not known, the related object |
| could be in any of the subclass tables, making joins through the relation |
| impossible. This ambiguity also affects identity lookups and queries; these |
| operations require multiple SQL <code class="literal">SELECT</code>s (one for each |
| possible subclass), or a complex <code class="literal">UNION</code>. |
| </p> |
| <div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3> |
| <p> |
| <a class="xref" href="ref_guide_mapping_limits.html#ref_guide_mapping_limits_tpc" title="8.1. Table Per Class">Section 8.1, “ |
| Table Per Class |
| ”</a> in the Reference Guide |
| describes the limitations OpenJPA places on table-per-class mapping. |
| </p> |
| </div> |
| </div> |
| </div> |
| <div class="section" title="6.4. Putting it All Together"><div class="titlepage"><div><div><h3 class="title" id="jpa_overview_mapping_inher_together">6.4. |
| Putting it All Together |
| </h3></div></div></div> |
| |
| <p> |
| Now that we have covered JPA's inheritance strategies, we can update our mapping |
| document with inheritance information. Here is the complete model: |
| </p> |
| <div class="mediaobject"><table border="0" summary="manufactured viewport for HTML img" cellspacing="0" cellpadding="0" width="341"><tr><td><img src="img/jpa-inher-all.png"></td></tr></table></div> |
| <p> |
| And here is the corresponding mapping metadata: |
| </p> |
| <div class="example"><a name="jpa_overview_mapping_inher_togetherex"></a><p class="title"><b>Example 13.8. |
| Inheritance Mapping |
| </b></p><div class="example-contents"> |
| |
| <pre class="programlisting"> |
| package org.mag; |
| |
| @Entity |
| @IdClass(Magazine.MagazineId.class) |
| @Table(name="MAG") |
| public class Magazine { |
| |
| @Column(length=9) |
| @Id private String isbn; |
| @Id private String title; |
| |
| ... |
| |
| public static class MagazineId { |
| ... |
| } |
| } |
| |
| @Entity |
| @Table(name="ART", uniqueConstraints=@Unique(columnNames="TITLE")) |
| @SequenceGenerator(name="ArticleSeq", sequenceName="ART_SEQ") |
| public class Article { |
| |
| @Id |
| @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ArticleSeq") |
| private long id; |
| |
| ... |
| } |
| |
| |
| package org.mag.pub; |
| |
| @Entity |
| @Table(name="COMP") |
| public class Company { |
| |
| @Column(name="CID") |
| @Id private long id; |
| |
| ... |
| } |
| |
| @Entity |
| @Table(name="AUTH") |
| public class Author { |
| |
| @Id |
| @GeneratedValue(strategy=GenerationType.TABLE, generator="AuthorGen") |
| @TableGenerator(name="AuthorGen", table="AUTH_GEN", pkColumnName="PK", |
| valueColumnName="AID") |
| @Column(name="AID", columnDefinition="INTEGER64") |
| private long id; |
| |
| ... |
| } |
| |
| @Embeddable |
| public class Address { |
| ... |
| } |
| |
| |
| package org.mag.subscribe; |
| |
| @MappedSuperclass |
| public abstract class Document { |
| |
| @Id |
| @GeneratedValue(strategy=GenerationType.IDENTITY) |
| private long id; |
| |
| ... |
| } |
| |
| @Entity |
| @Table(schema="CNTRCT") |
| @Inheritance(strategy=InheritanceType.JOINED) |
| public class Contract |
| extends Document { |
| ... |
| } |
| |
| @Entity |
| @Table(name="SUB", schema="CNTRCT") |
| @Inheritance(strategy=InheritanceType.SINGLE_TABLE) |
| public class Subscription { |
| |
| @Id |
| @GeneratedValue(strategy=GenerationType.IDENTITY) |
| private long id; |
| |
| ... |
| |
| @Entity |
| @Table(name="LINE_ITEM", schema="CNTRCT") |
| @PrimaryKeyJoinColumn(name="ID", referencedColumnName="ID") |
| public static class LineItem |
| extends Contract { |
| ... |
| } |
| } |
| |
| @Entity(name="Lifetime") |
| public class LifetimeSubscription |
| extends Subscription { |
| ... |
| } |
| |
| @Entity(name="Trial") |
| public class TrialSubscription |
| extends Subscription { |
| ... |
| } |
| </pre> |
| <p> |
| The same metadata expressed in XML form: |
| </p> |
| <pre class="programlisting"> |
| <entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd" |
| version="1.0"> |
| <mapped-superclass class="org.mag.subscribe.Document"> |
| <attributes> |
| <id name="id"> |
| <generated-value strategy="IDENTITY"/> |
| </id> |
| ... |
| </attributes> |
| </mapped-superclass> |
| <entity class="org.mag.Magazine"> |
| <table name="MAG"/> |
| <id-class="org.mag.Magazine.MagazineId"/> |
| <attributes> |
| <id name="isbn"> |
| <column length="9"/> |
| </id> |
| <id name="title"/> |
| ... |
| </attributes> |
| </entity> |
| <entity class="org.mag.Article"> |
| <table name="ART"> |
| <unique-constraint> |
| <column-name>TITLE</column-name> |
| </unique-constraint> |
| </table> |
| <sequence-generator name="ArticleSeq" sequence-name="ART_SEQ"/> |
| <attributes> |
| <id name="id"> |
| <generated-value strategy="SEQUENCE" generator="ArticleSeq"/> |
| </id> |
| ... |
| </attributes> |
| </entity> |
| <entity class="org.mag.pub.Company"> |
| <table name="COMP"/> |
| <attributes> |
| <id name="id"> |
| <column name="CID"/> |
| </id> |
| ... |
| </attributes> |
| </entity> |
| <entity class="org.mag.pub.Author"> |
| <table name="AUTH"/> |
| <attributes> |
| <id name="id"> |
| <column name="AID" column-definition="INTEGER64"/> |
| <generated-value strategy="TABLE" generator="AuthorGen"/> |
| <table-generator name="AuthorGen" table="AUTH_GEN" |
| pk-column-name="PK" value-column-name="AID"/> |
| </id> |
| ... |
| </attributes> |
| </entity> |
| <entity class="org.mag.subcribe.Contract"> |
| <table schema="CNTRCT"/> |
| <inheritance strategy="JOINED"/> |
| <attributes> |
| ... |
| </attributes> |
| </entity> |
| <entity class="org.mag.subcribe.Subscription"> |
| <table name="SUB" schema="CNTRCT"/> |
| <inheritance strategy="SINGLE_TABLE"/> |
| <attributes> |
| <id name="id"> |
| <generated-value strategy="IDENTITY"/> |
| </id> |
| ... |
| </attributes> |
| </entity> |
| <entity class="org.mag.subscribe.Subscription.LineItem"> |
| <table name="LINE_ITEM" schema="CNTRCT"/> |
| <primary-key-join-column name="ID" referenced-column-name="PK"/> |
| ... |
| </entity> |
| <entity class="org.mag.subscribe.LifetimeSubscription" name="Lifetime"> |
| ... |
| </entity> |
| <entity class="org.mag.subscribe.TrialSubscription" name="Trial"> |
| ... |
| </entity> |
| </entity-mappings> |
| </pre> |
| </div></div><br class="example-break"> |
| </div> |
| </div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="jpa_overview_mapping_sequence.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="jpa_overview_mapping.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="jpa_overview_mapping_discrim.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">5. |
| Generators |
| </td><td width="20%" align="center"><a accesskey="h" href="manual.html">Home</a></td><td width="40%" align="right" valign="top"> 7. |
| Discriminator |
| </td></tr></table></div></body></html> |