Added: websites/production/openjpa/content/builds/2.4.2/apache-openjpa/docs/jpa_overview_mapping_inher.html ============================================================================== --- websites/production/openjpa/content/builds/2.4.2/apache-openjpa/docs/jpa_overview_mapping_inher.html (added) +++ websites/production/openjpa/content/builds/2.4.2/apache-openjpa/docs/jpa_overview_mapping_inher.html Fri Jan 6 19:19:20 2017 @@ -0,0 +1,712 @@ +<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 Stylesheets V1.79.1"><link rel="home" href="manual.html" title="Apache OpenJPA 2.4 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" id="jpa_overview_mapping_inher"><div class="titlepage"><div><div><h2 class="title" style="clear: both">6. + Inheritance + </h2></div></div></div><div class="toc"><dl class="toc"><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" style="list-style-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" style="list-style-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" 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" id="jpa_overview_mapping_inher_single"><div class="titlepage"><div><div><h3 class="title">6.1. + Single Table + </h3></div></div></div><div class="toc"><dl class="toc"><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" style="cellpadding: 0; cellspacing: 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" id="jpa_overview_mapping_inher_singleex"><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" 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" id="jpa_overview_mapping_inher_single_adv"><div class="titlepage"><div><div><h4 class="title">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" id="jpa_overview_mapping_inher_single_disadv"><div class="titlepage"><div><div><h4 class="title">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" id="jpa_overview_mapping_inher_joined"><div class="titlepage"><div><div><h3 class="title">6.2. + Joined + </h3></div></div></div><div class="toc"><dl class="toc"><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" 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" style="cellpadding: 0; cellspacing: 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" style="list-style-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" style="list-style-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" id="jpa_overview_mapping_inher_joinedex"><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" id="jpa_overview_mapping_inher_joined_adv"><div class="titlepage"><div><div><h4 class="title">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" id="jpa_overview_mapping_inher_joined_disadv"><div class="titlepage"><div><div><h4 class="title">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" 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" id="jpa_overview_mapping_inher_tpc"><div class="titlepage"><div><div><h3 class="title">6.3. + Table Per Class + </h3></div></div></div><div class="toc"><dl class="toc"><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" style="cellpadding: 0; cellspacing: 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" id="jpa_overview_mapping_inher_tpcex"><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" id="jpa_overview_mapping_inher_tpc_adv"><div class="titlepage"><div><div><h4 class="title">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" id="jpa_overview_mapping_inher_tpc_disadv"><div class="titlepage"><div><div><h4 class="title">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" 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" id="jpa_overview_mapping_inher_together"><div class="titlepage"><div><div><h3 class="title">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" style="cellpadding: 0; cellspacing: 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" id="jpa_overview_mapping_inher_togetherex"><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> \ No newline at end of file
Added: websites/production/openjpa/content/builds/2.4.2/apache-openjpa/docs/jpa_overview_mapping_sequence.html ============================================================================== --- websites/production/openjpa/content/builds/2.4.2/apache-openjpa/docs/jpa_overview_mapping_sequence.html (added) +++ websites/production/openjpa/content/builds/2.4.2/apache-openjpa/docs/jpa_overview_mapping_sequence.html Fri Jan 6 19:19:20 2017 @@ -0,0 +1,411 @@ +<html><head> + <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> + <title>5. Generators</title><base href="display"><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"><link rel="home" href="manual.html" title="Apache OpenJPA 2.4 User's Guide"><link rel="up" href="jpa_overview_mapping.html" title="Chapter 13. Mapping Metadata"><link rel="prev" href="jpa_overview_mapping_id.html" title="4. Identity Mapping"><link rel="next" href="jpa_overview_mapping_inher.html" title="6. Inheritance"></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">5. + Generators + </th></tr><tr><td width="20%" align="left"><a accesskey="p" href="jpa_overview_mapping_id.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_inher.html">Next</a></td></tr></table><hr></div><div class="section" id="jpa_overview_mapping_sequence"><div class="titlepage"><div><div><h2 class="title" style="clear: both">5. + Generators + </h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="section"><a href="jpa_overview_mapping_sequence.html#jpa_overview_mapping_sequence_seqgen">5.1. + Sequence Generator + </a></span></dt><dt><span class="section"><a href="jpa_overview_mapping_sequence.html#jpa_overview_mapping_sequence_tablegen">5.2. + Table Generator + </a></span></dt><dt><span class="section"><a href="jpa_overview_mapping_sequence.html#jpa_overview_mapping_sequence_genex">5.3. + Example + </a></span></dt></dl></div> + + <a class="indexterm" name="d5e5656"></a> + <a class="indexterm" name="d5e5659"></a> + <p> +One aspect of identity mapping not covered in the previous section is JPA's +ability to automatically assign a value to your numeric identity fields using +<span class="emphasis"><em>generators</em></span>. We discussed the available generator types in +<a class="xref" href="jpa_overview_meta_field.html#jpa_overview_meta_id" title="2.3. Id">Section 2.3, “ + Id + ”</a>. Now we show you how to define +named generators. + </p> + <div class="section" id="jpa_overview_mapping_sequence_seqgen"><div class="titlepage"><div><div><h3 class="title">5.1. + Sequence Generator + </h3></div></div></div> + + <a class="indexterm" name="d5e5669"></a> + <a class="indexterm" name="d5e5672"></a> + <p> +Most databases allow you to create native sequences. These are database +structures that generate increasing numeric values. The <code class="classname"> +SequenceGenerator</code> annotation represents a named database sequence. +You can place the annotation on any package, entity class, persistent field +declaration (if your entity uses field access), or getter method for a +persistent property (if your entity uses property access). <code class="classname"> +SequenceGenerator</code> has the following properties: + </p> + <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> + <p> + <a class="indexterm" name="d5e5680"></a> +<code class="literal">String name</code>: The generator name. This property is required. + </p> + </li><li class="listitem"> + <p> + <a class="indexterm" name="d5e5686"></a> +<code class="literal">String sequenceName</code>: The name of the database sequence. If +you do not specify the database sequence, your vendor will choose an appropriate +default. + </p> + </li><li class="listitem"> + <p> + <a class="indexterm" name="d5e5692"></a> +<code class="literal">int initialValue</code>: The initial sequence value. + </p> + </li><li class="listitem"> + <p> + <a class="indexterm" name="d5e5698"></a> +<code class="literal">int allocationSize</code>: The number of values to allocate in +memory for each trip to the database. Allocating values in memory allows the JPA +runtime to avoid accessing the database for every sequence request. +This number also specifies the amount that the sequence value is incremented +each time the sequence is accessed. Defaults to 50. + </p> + </li><li class="listitem"> + <p> + <a class="indexterm" name="d5e5704"></a> + <code class="literal">String schema</code>: The sequence's schema. If you do not name a +schema, JPA uses the default schema for the database connection. + </p> + </li></ul></div> + <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3> + <p> +OpenJPA allows you to use one of OpenJPA's built-in generator +implementations in the <code class="literal">sequenceName</code> property. You can also +set the <code class="literal">sequenceName</code> to <code class="literal">system</code> to use the +system sequence defined by the <a class="link" href="ref_guide_conf_openjpa.html#openjpa.Sequence" title="5.67. openjpa.Sequence"><code class="literal"> +openjpa.Sequence</code></a> configuration property. See the Reference +Guide's <a class="xref" href="ref_guide_sequence.html" title="6. Generators">Section 6, “ + Generators + ”</a> for details. + </p> + </div> + <p> +The XML element for a sequence generator is <code class="literal">sequence-generator +</code>. Its attributes mirror the above annotation's properties: + </p> + <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> + <p> +<code class="literal">name</code> + </p> + </li><li class="listitem"> + <p> +<code class="literal">sequence-name</code> + </p> + </li><li class="listitem"> + <p> +<code class="literal">initial-value</code> + </p> + </li><li class="listitem"> + <p> +<code class="literal">allocation-size</code> + </p> + </li><li class="listitem"> + <p> +<code class="literal">schema</code> + </p> + </li></ul></div> + <p> +To use a sequence generator, set your <code class="classname">GeneratedValue</code> +annotation's <code class="literal">strategy</code> property to <code class="literal"> +GenerationType.SEQUENCE</code>, and its <code class="literal">generator</code> property +to the sequence generator's declared name. Or equivalently, set your <code class="literal"> +generated-value</code> XML element's <code class="literal">strategy</code> attribute to +<code class="literal">SEQUENCE</code> and its <code class="literal">generator</code> attribute to +the generator name. + </p> + </div> + <div class="section" id="jpa_overview_mapping_sequence_tablegen"><div class="titlepage"><div><div><h3 class="title">5.2. + Table Generator + </h3></div></div></div> + + <a class="indexterm" name="d5e5745"></a> + <a class="indexterm" name="d5e5748"></a> + <p> +A <code class="classname">TableGenerator</code> refers to a database table used to store +increasing sequence values for one or more entities. As with <code class="classname"> +SequenceGenerator</code>, you can place the <code class="classname">TableGenerator +</code> annotation on any package, entity class, persistent field +declaration (if your entity uses field access), or getter method for a +persistent property (if your entity uses property access). <code class="classname"> +TableGenerator</code> has the following properties: + </p> + <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> + <p> + <a class="indexterm" name="d5e5758"></a> +<code class="literal">String name</code>: The generator name. This property is required. + </p> + </li><li class="listitem"> + <p> + <a class="indexterm" name="d5e5764"></a> +<code class="literal">String table</code>: The name of the generator table. If left +unspecified, your vendor will choose a default table. + </p> + </li><li class="listitem"> + <p> + <a class="indexterm" name="d5e5770"></a> +<code class="literal">String schema</code>: The named table's schema. + </p> + </li><li class="listitem"> + <p> + <a class="indexterm" name="d5e5776"></a> +<code class="literal">String catalog</code>: The named table's catalog. + </p> + </li><li class="listitem"> + <p> + <a class="indexterm" name="d5e5782"></a> +<code class="literal">String pkColumnName</code>: The name of the primary key column in +the generator table. If unspecified, your implementation will choose a default. + </p> + </li><li class="listitem"> + <p> + <a class="indexterm" name="d5e5788"></a> +<code class="literal">String valueColumnName</code>: The name of the column that holds +the sequence value. If unspecified, your implementation will choose a default. + </p> + </li><li class="listitem"> + <p> + <a class="indexterm" name="d5e5794"></a> +<code class="literal">String pkColumnValue</code>: The primary key column value of the +row in the generator table holding this sequence value. You can use the same +generator table for multiple logical sequences by supplying different <code class="literal"> +pkColumnValue</code> s. If you do not specify a value, the implementation +will supply a default. + </p> + </li><li class="listitem"> + <p> + <a class="indexterm" name="d5e5801"></a> +<code class="literal">int initialValue</code>: The value of the generator's first issued +number. + </p> + </li><li class="listitem"> + <p> + <a class="indexterm" name="d5e5807"></a> +<code class="literal">int allocationSize</code>: The number of values to allocate in +memory for each trip to the database. Allocating values in memory allows the JPA +runtime to avoid accessing the database for every sequence request. +This number also specifies the amount that the sequence value is incremented +each time the generator table is updated. Defaults to 50. + </p> + </li></ul></div> + <p> +The XML equivalent is the <code class="literal">table-generator</code> element. This +element's attributes correspond exactly to the above annotation's properties: + </p> + <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> + <p> +<code class="literal">name</code> + </p> + </li><li class="listitem"> + <p> +<code class="literal">table</code> + </p> + </li><li class="listitem"> + <p> +<code class="literal">schema</code> + </p> + </li><li class="listitem"> + <p> +<code class="literal">catalog</code> + </p> + </li><li class="listitem"> + <p> +<code class="literal">pk-column-name</code> + </p> + </li><li class="listitem"> + <p> +<code class="literal">value-column-name</code> + </p> + </li><li class="listitem"> + <p> +<code class="literal">pk-column-value</code> + </p> + </li><li class="listitem"> + <p> +<code class="literal">initial-value</code> + </p> + </li><li class="listitem"> + <p> +<code class="literal">allocation-size</code> + </p> + </li></ul></div> + <p> +To use a table generator, set your <code class="classname">GeneratedValue</code> +annotation's <code class="literal">strategy</code> property to <code class="literal"> +GenerationType.TABLE</code>, and its <code class="literal">generator</code> property to +the table generator's declared name. Or equivalently, set your <code class="literal"> +generated-value</code> XML element's <code class="literal">strategy</code> attribute to +<code class="literal">TABLE</code> and its <code class="literal">generator</code> attribute to the +generator name. + </p> + </div> + <div class="section" id="jpa_overview_mapping_sequence_genex"><div class="titlepage"><div><div><h3 class="title">5.3. + Example + </h3></div></div></div> + + <p> +Let's take advantage of generators in our entity model. Here are our updated +mappings. + </p> + <div class="example" id="jpa_overview_mapping_sequenceex"><p class="title"><b>Example 13.4. + Generator 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(generate=GenerationType.IDENTITY) + private long id; + + ... +} + +@Entity +@Table(schema="CNTRCT") +public class Contract + extends Document { + + ... +} + +@Entity +@Table(name="SUB", schema="CNTRCT") +public class Subscription { + + @Id + @GeneratedValue(strategy=GenerationType.IDENTITY) + private long id; + + ... + + @Entity + @Table(name="LINE_ITEM", schema="CNTRCT") + 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 for <code class="literal">Article</code> and <code class="literal">Author</code> +expressed in XML form: + </p> +<pre class="programlisting"> +<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.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> +</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_id.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_inher.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">4. + Identity Mapping + </td><td width="20%" align="center"><a accesskey="h" href="manual.html">Home</a></td><td width="40%" align="right" valign="top"> 6. + Inheritance + </td></tr></table></div></body></html> \ No newline at end of file Added: websites/production/openjpa/content/builds/2.4.2/apache-openjpa/docs/jpa_overview_mapping_unq.html ============================================================================== --- websites/production/openjpa/content/builds/2.4.2/apache-openjpa/docs/jpa_overview_mapping_unq.html (added) +++ websites/production/openjpa/content/builds/2.4.2/apache-openjpa/docs/jpa_overview_mapping_unq.html Fri Jan 6 19:19:20 2017 @@ -0,0 +1,84 @@ +<html><head> + <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> + <title>2. Unique Constraints</title><base href="display"><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"><link rel="home" href="manual.html" title="Apache OpenJPA 2.4 User's Guide"><link rel="up" href="jpa_overview_mapping.html" title="Chapter 13. Mapping Metadata"><link rel="prev" href="jpa_overview_mapping.html" title="Chapter 13. Mapping Metadata"><link rel="next" href="jpa_overview_mapping_column.html" title="3. Column"></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">2. + Unique Constraints + </th></tr><tr><td width="20%" align="left"><a accesskey="p" href="jpa_overview_mapping.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_column.html">Next</a></td></tr></table><hr></div><div class="section" id="jpa_overview_mapping_unq"><div class="titlepage"><div><div><h2 class="title" style="clear: both">2. + Unique Constraints + </h2></div></div></div> + + <a class="indexterm" name="d5e5471"></a> + <a class="indexterm" name="d5e5475"></a> + <p> +Unique constraints ensure that the data in a column or combination of columns is +unique for each row. A table's primary key, for example, functions as an +implicit unique constraint. In JPA, you represent other unique +constraints with an array of <code class="classname"> UniqueConstraint</code> +annotations within the table annotation. The unique constraints you define are +used during table creation to generate the proper database constraints, and may +also be used at runtime to order <code class="literal">INSERT</code>, <code class="literal">UPDATE +</code>, and <code class="literal">DELETE</code> statements. For example, suppose there +is a unique constraint on the columns of field <code class="literal">F</code>. In the +same transaction, you remove an object <code class="literal">A</code> and persist a new +object <code class="literal">B</code>, both with the same <code class="literal">F</code> value. The +JPA runtime must ensure that the SQL deleting <code class="literal">A</code> +is sent to the database before the SQL inserting <code class="literal">B</code> to avoid a +unique constraint violation. + </p> + <p> +<code class="classname">UniqueConstraint</code> has these properties: + </p> + <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> + <p> +<code class="literal">String name</code>: The name of the constraint. OpenJPA will choose +a name if you do not provide one, or will create an anonymous constraint. + </p> + </li><li class="listitem"> + <p> +<code class="literal">String[] columnNames</code>: The names of the columns the +constraint spans. + </p> + </li></ul></div> + <p> +In XML, unique constraints are represented by nesting <code class="literal"> +unique-constraint</code> elements within the <code class="literal"> table</code> +element. Each <code class="literal">unique-constraint</code> element in turn nests +<code class="literal">column-name</code> text elements to enumerate the constraint's +columns. + </p> + <div class="example" id="jpa_overview_mapping_unq_attrex"><p class="title"><b>Example 13.2. + Defining a Unique Constraint + </b></p><div class="example-contents"> + + <p> +The following defines a unique constraint on the <code class="literal"> TITLE</code> +column of the <code class="literal">ART</code> table: + </p> +<pre class="programlisting"> +@Entity +@Table(name="ART", uniqueConstraints=@UniqueConstraint(name="TITLE_CNSTR", columnNames="TITLE")) +public class Article { + ... +} +</pre> + <p> +The same metadata expressed in XML form: + </p> +<pre class="programlisting"> +<entity class="org.mag.Article"> + <table name="ART"> + <unique-constraint> + <name>TITLE_CNSTR</name> + <column-name>TITLE</column-name> + </unique-constraint> + </table> + ... +</entity> +</pre> + </div></div><br class="example-break"> + </div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="jpa_overview_mapping.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_column.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 13. + Mapping Metadata + </td><td width="20%" align="center"><a accesskey="h" href="manual.html">Home</a></td><td width="40%" align="right" valign="top"> 3. + Column + </td></tr></table></div></body></html> \ No newline at end of file Added: websites/production/openjpa/content/builds/2.4.2/apache-openjpa/docs/jpa_overview_meta.html ============================================================================== --- websites/production/openjpa/content/builds/2.4.2/apache-openjpa/docs/jpa_overview_meta.html (added) +++ websites/production/openjpa/content/builds/2.4.2/apache-openjpa/docs/jpa_overview_meta.html Fri Jan 6 19:19:20 2017 @@ -0,0 +1,496 @@ +<html><head> + <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> + <title>Chapter 5. Metadata</title><base href="display"><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"><link rel="home" href="manual.html" title="Apache OpenJPA 2.4 User's Guide"><link rel="up" href="jpa_overview.html" title="Part 2. Java Persistence API"><link rel="prev" href="jpa_overview_pc_conclusion.html" title="4. Conclusions"><link rel="next" href="jpa_overview_meta_field.html" title="2. Field and Property Metadata"></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">Chapter 5. + Metadata + </th></tr><tr><td width="20%" align="left"><a accesskey="p" href="jpa_overview_pc_conclusion.html">Prev</a> </td><th width="60%" align="center">Part 2. Java Persistence API</th><td width="20%" align="right"> <a accesskey="n" href="jpa_overview_meta_field.html">Next</a></td></tr></table><hr></div><div class="chapter" id="jpa_overview_meta"><div class="titlepage"><div><div><h2 class="title">Chapter 5. + Metadata + </h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl class="toc"><dt><span class="section"><a href="jpa_overview_meta.html#jpa_overview_meta_class">1. + Class Metadata + </a></span></dt><dd><dl><dt><span class="section"><a href="jpa_overview_meta.html#jpa_overview_meta_entity">1.1. + Entity + </a></span></dt><dt><span class="section"><a href="jpa_overview_meta.html#jpa_overview_meta_idclass">1.2. + Id Class + </a></span></dt><dt><span class="section"><a href="jpa_overview_meta.html#jpa_overview_meta_embeddablesuper">1.3. + Mapped Superclass + </a></span></dt><dt><span class="section"><a href="jpa_overview_meta.html#jpa_overview_meta_embeddable">1.4. + Embeddable + </a></span></dt><dt><span class="section"><a href="jpa_overview_meta.html#jpa_overview_meta_entity_listeners">1.5. + EntityListeners + </a></span></dt><dt><span class="section"><a href="jpa_overview_meta.html#jpa_overview_meta_classex">1.6. + Example + </a></span></dt></dl></dd><dt><span class="section"><a href="jpa_overview_meta_field.html">2. + Field and Property Metadata + </a></span></dt><dd><dl><dt><span class="section"><a href="jpa_overview_meta_field.html#jpa_overview_explicit_access">2.1. + Explicit Access + </a></span></dt><dt><span class="section"><a href="jpa_overview_meta_field.html#jpa_overview_meta_transient">2.2. + Transient + </a></span></dt><dt><span class="section"><a href="jpa_overview_meta_field.html#jpa_overview_meta_id">2.3. + Id + </a></span></dt><dt><span class="section"><a href="jpa_overview_meta_field.html#jpa_overview_meta_gen">2.4. + Generated Value + </a></span></dt><dt><span class="section"><a href="jpa_overview_meta_field.html#jpa_overview_meta_embedid">2.5. + Embedded Id + </a></span></dt><dt><span class="section"><a href="jpa_overview_meta_field.html#jpa_overview_meta_version">2.6. + Version + </a></span></dt><dt><span class="section"><a href="jpa_overview_meta_field.html#jpa_overview_meta_basic">2.7. + Basic + </a></span></dt><dd><dl><dt><span class="section"><a href="jpa_overview_meta_field.html#jpa_overview_meta_fetch">2.7.1. + Fetch Type + </a></span></dt></dl></dd><dt><span class="section"><a href="jpa_overview_meta_field.html#jpa_overview_meta_embedded">2.8. + Embedded + </a></span></dt><dt><span class="section"><a href="jpa_overview_meta_field.html#jpa_overview_meta_manytoone">2.9. + Many To One + </a></span></dt><dd><dl><dt><span class="section"><a href="jpa_overview_meta_field.html#jpa_overview_meta_cascade">2.9.1. + Cascade Type + </a></span></dt></dl></dd><dt><span class="section"><a href="jpa_overview_meta_field.html#jpa_overview_meta_onetomany">2.10. + One To Many + </a></span></dt><dd><dl><dt><span class="section"><a href="jpa_overview_meta_field.html#jpa_overview_meta_mappedby">2.10.1. + Bidirectional Relations + </a></span></dt></dl></dd><dt><span class="section"><a href="jpa_overview_meta_field.html#jpa_overview_meta_onetoone">2.11. + One To One + </a></span></dt><dt><span class="section"><a href="jpa_overview_meta_field.html#jpa_overview_meta_manytomany">2.12. + Many To Many + </a></span></dt><dt><span class="section"><a href="jpa_overview_meta_field.html#jpa_overview_meta_orderby">2.13. + Order By + </a></span></dt><dt><span class="section"><a href="jpa_overview_meta_field.html#jpa_overview_meta_mapkey">2.14. + Map Key + </a></span></dt><dt><span class="section"><a href="jpa_overview_meta_field.html#jpa_overview_meta_fielddefaults">2.15. + Persistent Field Defaults + </a></span></dt></dl></dd><dt><span class="section"><a href="jpa_overview_meta_xml.html">3. + XML Schema + </a></span></dt><dt><span class="section"><a href="jpa_overview_meta_complete.html">4. + Conclusion + </a></span></dt></dl></div> + + <a class="indexterm" name="d5e996"></a> + <a class="indexterm" name="d5e998"></a> + <p> +JPA requires that you accompany each persistent class with persistence metadata. +This metadata serves three primary purposes: + </p> + <div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"> + <p> +To identify persistent classes. + </p> + </li><li class="listitem"> + <p> +To override default JPA behavior. + </p> + </li><li class="listitem"> + <p> +To provide the JPA implementation with information that it cannot glean from +simply reflecting on the persistent class. + </p> + </li></ol></div> + <p> + <a class="indexterm" name="d5e1011"></a> +Persistence metadata is specified using either the Java annotations defined in +the <code class="literal">javax.persistence</code> package, XML mapping files, or a +mixture of both. In the latter case, XML declarations override conflicting +annotations. If you choose to use XML metadata, the XML files must be available +at development and runtime, and must be discoverable via either of two +strategies: + </p> + <div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"> + <p> +In a resource named <code class="filename">orm.xml</code> placed in a <code class="filename"> +META-INF</code> directory within a directory in your classpath or within a +jar archive containing your persistent classes. + </p> + </li><li class="listitem"> + <p> +Declared in your <a class="link" href="jpa_overview_persistence.html#jpa_overview_persistence_xml" title="1. persistence.xml"><code class="filename"> +persistence.xml</code></a> configuration file. In this case, each XML +metadata file must be listed in a <code class="literal">mapping-file</code> element whose +content is either a path to the given file or a resource location available to +the class' class loader. + </p> + </li></ol></div> + <p> +We describe the standard metadata annotations and XML equivalents throughout +this chapter. The full schema for XML mapping files is available in +<a class="xref" href="jpa_overview_meta_xml.html" title="3. XML Schema">Section 3, “ + XML Schema + ”</a>. JPA also standardizes relational +mapping metadata and named query metadata, which we discuss in +<a class="xref" href="jpa_overview_mapping.html" title="Chapter 13. Mapping Metadata">Chapter 13, <i> + Mapping Metadata + </i></a> and +<a class="xref" href="jpa_overview_query.html#jpa_overview_query_named" title="1.11. Named Queries">Section 1.11, “ + Named Queries + ”</a> respectively. + </p> + <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3> + <p> +OpenJPA defines many useful annotations beyond the standard set. See +<a class="xref" href="ref_guide_meta_jpa.html" title="3. Additional JPA Metadata">Section 3, “ + Additional JPA Metadata + ”</a> and +<a class="xref" href="ref_guide_meta_ext.html" title="4. Metadata Extensions">Section 4, “ + Metadata Extensions + ”</a> +in the Reference Guide for details. There are currently no XML equivalents for +these extension annotations. + </p> + </div> + <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3> + <p> +Persistence metadata may be used to validate the contents of your entities prior to communicating +with the database. This differs from mapping meta data which is primarily used for schema generation. +For example if you indicate that a relationship is not optional (e.g. @Basic(optional=false)) OpenJPA +will validate that the variable in your entity is not null <span class="emphasis"><em>before</em></span> inserting a row +in the database. + </p> + </div> + <div class="mediaobject"><table border="0" summary="manufactured viewport for HTML img" style="cellpadding: 0; cellspacing: 0;" width="369"><tr><td><img src="img/jpa-meta-model.png"></td></tr></table></div> + <p> +Through the course of this chapter, we will create the persistent object model +above. + </p> + <div class="section" id="jpa_overview_meta_class"><div class="titlepage"><div><div><h2 class="title" style="clear: both">1. + Class Metadata + </h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="section"><a href="jpa_overview_meta.html#jpa_overview_meta_entity">1.1. + Entity + </a></span></dt><dt><span class="section"><a href="jpa_overview_meta.html#jpa_overview_meta_idclass">1.2. + Id Class + </a></span></dt><dt><span class="section"><a href="jpa_overview_meta.html#jpa_overview_meta_embeddablesuper">1.3. + Mapped Superclass + </a></span></dt><dt><span class="section"><a href="jpa_overview_meta.html#jpa_overview_meta_embeddable">1.4. + Embeddable + </a></span></dt><dt><span class="section"><a href="jpa_overview_meta.html#jpa_overview_meta_entity_listeners">1.5. + EntityListeners + </a></span></dt><dt><span class="section"><a href="jpa_overview_meta.html#jpa_overview_meta_classex">1.6. + Example + </a></span></dt></dl></div> + + <p> +The following metadata annotations and XML elements apply to persistent class +declarations. + </p> + <div class="section" id="jpa_overview_meta_entity"><div class="titlepage"><div><div><h3 class="title">1.1. + Entity + </h3></div></div></div> + + <a class="indexterm" name="d5e1044"></a> + <a class="indexterm" name="d5e1047"></a> + <a class="indexterm" name="d5e1050"></a> + <p> +The <code class="classname">Entity</code> annotation denotes an entity class. All entity +classes must have this annotation. The <code class="classname">Entity</code> annotation +takes one optional property: + </p> + <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> + <p> +<code class="literal">String name</code>: Name used to refer to the entity in queries. +Must not be a reserved literal in JPQL. Defaults to the unqualified name of the +entity class. + </p> + </li></ul></div> + <p> +The equivalent XML element is <code class="literal">entity</code>. It has the following +attributes: + </p> + <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> + <p> +<code class="literal">class</code>: The entity class. This attribute is required. + </p> + </li><li class="listitem"> + <p> +<code class="literal">name</code>: Named used to refer to the class in queries. See the +name property above. + </p> + </li><li class="listitem"> + <p> +<code class="literal">access</code>: The access type to use for the class. Must either be +<code class="literal">FIELD</code> or <code class="literal">PROPERTY</code>. For details on access +types, see <a class="xref" href="jpa_overview_meta_field.html" title="2. Field and Property Metadata">Section 2, “ + Field and Property Metadata + ”</a>. + </p> + </li></ul></div> + <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3> + <p> +OpenJPA uses a process called <span class="emphasis"><em>enhancement</em></span> to modify the +bytecode of entities for transparent lazy loading and immediate dirty tracking. +See <a class="xref" href="ref_guide_pc_enhance.html" title="2. Enhancement">Section 2, “ + Enhancement + ”</a> in the Reference Guide for +details on enhancement. + </p> + </div> + </div> + <div class="section" id="jpa_overview_meta_idclass"><div class="titlepage"><div><div><h3 class="title">1.2. + Id Class + </h3></div></div></div> + + <a class="indexterm" name="d5e1081"></a> + <a class="indexterm" name="d5e1083"></a> + <a class="indexterm" name="d5e1086"></a> + <p> +As we discussed in <a class="xref" href="jpa_overview_pc_identity.html#jpa_overview_pc_identitycls" title="2.1. Identity Class">Section 2.1, “ + Identity Class + ”</a>, +entities with multiple identity fields must use an <span class="emphasis"><em> identity class +</em></span> to encapsulate their persistent identity. The <code class="classname">IdClass +</code> annotation specifies this class. It accepts a single <code class="classname"> +java.lang.Class</code> value. + </p> + <p> +The equivalent XML element is <code class="literal">id-class</code>, which has a single +attribute: + </p> + <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> + <p> +<code class="literal">class</code>: Set this required attribute to the name of the +identity class. + </p> + </li></ul></div> + </div> + <div class="section" id="jpa_overview_meta_embeddablesuper"><div class="titlepage"><div><div><h3 class="title">1.3. + Mapped Superclass + </h3></div></div></div> + + <a class="indexterm" name="d5e1102"></a> + <a class="indexterm" name="d5e1104"></a> + <a class="indexterm" name="d5e1107"></a> + <p> +A <span class="emphasis"><em>mapped superclass</em></span> is a non-entity class that can define +persistent state and mapping information for entity subclasses. Mapped +superclasses are usually abstract. Unlike true entities, you cannot query a +mapped superclass, pass a mapped superclass instance to any <code class="classname"> +EntityManager</code> or <code class="classname">Query</code> methods, or declare a +persistent relation with a mapped superclass target. You denote a mapped +superclass with the <code class="classname">MappedSuperclass</code> marker annotation. + </p> + <p> +The equivalent XML element is <code class="literal">mapped-superclass</code>. It expects +the following attributes: + </p> + <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> + <p> +<code class="literal">class</code>: The entity class. This attribute is required. + </p> + </li><li class="listitem"> + <p> +<code class="literal">access</code>: The access type to use for the class. Must either be +<code class="literal">FIELD</code> or <code class="literal">PROPERTY</code>. For details on access +types, see <a class="xref" href="jpa_overview_meta_field.html" title="2. Field and Property Metadata">Section 2, “ + Field and Property Metadata + ”</a>. + </p> + </li></ul></div> + <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3> + <p> +OpenJPA allows you to query on mapped superclasses. A query on a mapped +superclass will return all matching subclass instances. OpenJPA also allows you +to declare relations to mapped superclass types; however, you cannot query +across these relations. + </p> + </div> + </div> + <div class="section" id="jpa_overview_meta_embeddable"><div class="titlepage"><div><div><h3 class="title">1.4. + Embeddable + </h3></div></div></div> + + <a class="indexterm" name="d5e1131"></a> + <a class="indexterm" name="d5e1133"></a> + <a class="indexterm" name="d5e1136"></a> + <p> +The <code class="classname">Embeddable</code> annotation designates an embeddable +persistent class. Embeddable instances are stored as part of the record of their +owning instance. All embeddable classes must have this annotation. + </p> + <p> +A persistent class can either be an entity or an embeddable class, but not both. + </p> + <p> +The equivalent XML element is <code class="literal">embeddable</code>. It understands the +following attributes: + </p> + <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> + <p> +<code class="literal">class</code>: The entity class. This attribute is required. + </p> + </li><li class="listitem"> + <p> +<code class="literal">access</code>: The access type to use for the class. Must either be +<code class="literal">FIELD</code> or <code class="literal">PROPERTY</code>. For details on access +types, see <a class="xref" href="jpa_overview_meta_field.html" title="2. Field and Property Metadata">Section 2, “ + Field and Property Metadata + ”</a>. + </p> + </li></ul></div> + <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3> + <p> +OpenJPA allows a persistent class to be both an entity and an embeddable class. +Instances of the class will act as entities when persisted explicitly or assigned +to non-embedded fields of entities. Instances will act as embedded values when +assigned to embedded fields of entities. + </p> + <p> +To signal that a class is both an entity and an embeddable class in OpenJPA, +simply add both the <code class="literal">@Entity</code> and the <code class="literal">@Embeddable +</code> annotations to the class. + </p> + </div> + </div> + <div class="section" id="jpa_overview_meta_entity_listeners"><div class="titlepage"><div><div><h3 class="title">1.5. + EntityListeners + </h3></div></div></div> + + <a class="indexterm" name="d5e1161"></a> + <a class="indexterm" name="d5e1163"></a> + <a class="indexterm" name="d5e1165"></a> + <a class="indexterm" name="d5e1168"></a> + <p> +An entity may list its lifecycle event listeners in the <code class="classname"> +EntityListeners</code> annotation. This value of this annotation is an +array of the listener <code class="classname">Class</code> es for the entity. The +equivalent XML element is <code class="literal">entity-listeners</code>. For more details +on entity listeners, see <a class="xref" href="jpa_overview_pc_callbacks.html" title="3. Lifecycle Callbacks">Section 3, “ + Lifecycle Callbacks + ”</a>. + </p> + </div> + <div class="section" id="jpa_overview_meta_classex"><div class="titlepage"><div><div><h3 class="title">1.6. + Example + </h3></div></div></div> + + <p> +Here are the class declarations for our persistent object model, annotated with +the appropriate persistence metadata. Note that <code class="classname">Magazine</code> +declares an identity class, and that <code class="classname">Document</code> and +<code class="classname">Address</code> are a mapped superclass and an embeddable class, +respectively. <code class="classname">LifetimeSubscription</code> and <code class="classname"> +TrialSubscription</code> override the default entity name to supply a +shorter alias for use in queries. + </p> + <div class="example" id="jpa_overview_meta_classlisting"><p class="title"><b>Example 5.1. + Class Metadata + </b></p><div class="example-contents"> + +<pre class="programlisting"> +package org.mag; + +@Entity +@IdClass(Magazine.MagazineId.class) +public class Magazine { + ... + + public static class MagazineId { + ... + } +} + +@Entity +public class Article { + ... +} + + +package org.mag.pub; + +@Entity +public class Company { + ... +} + +@Entity +public class Author { + ... +} + +@Embeddable +public class Address { + ... +} + + +package org.mag.subscribe; + +@MappedSuperclass +public abstract class Document { + ... +} + +@Entity +public class Contract + extends Document { + ... +} + +@Entity +public class Subscription { + ... + + @Entity + 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 equivalent declarations in XML: + </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"> + ... + </mapped-superclass> + <entity class="org.mag.Magazine"> + <id-class class="org.mag.Magazine$MagazineId"/> + ... + </entity> + <entity class="org.mag.Article"> + ... + </entity> + <entity class="org.mag.pub.Company"> + ... + </entity> + <entity class="org.mag.pub.Author"> + ... + </entity> + <entity class="org.mag.subscribe.Contract"> + ... + </entity> + <entity class="org.mag.subscribe.LineItem"> + ... + </entity> + <entity class="org.mag.subscribe.LifetimeSubscription" name="Lifetime"> + ... + </entity> + <entity class="org.mag.subscribe.TrialSubscription" name="Trial"> + ... + </entity> + <embeddable class="org.mag.pub.Address"> + ... + </embeddable> +</entity-mappings> +</pre> + </div></div><br class="example-break"> + </div> + </div> + + + +</div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="jpa_overview_pc_conclusion.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="jpa_overview.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="jpa_overview_meta_field.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">4. + Conclusions + </td><td width="20%" align="center"><a accesskey="h" href="manual.html">Home</a></td><td width="40%" align="right" valign="top"> 2. + Field and Property Metadata + </td></tr></table></div></body></html> \ No newline at end of file