| <html><head> |
| <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
| <title>4. Identity Mapping</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.4 User's Guide"><link rel="up" href="jpa_overview_mapping.html" title="Chapter 13. Mapping Metadata"><link rel="prev" href="jpa_overview_mapping_column.html" title="3. Column"><link rel="next" href="jpa_overview_mapping_sequence.html" title="5. Generators"></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">4. |
| Identity Mapping |
| </th></tr><tr><td width="20%" align="left"><a accesskey="p" href="jpa_overview_mapping_column.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_sequence.html">Next</a></td></tr></table><hr></div><div class="section" title="4. Identity Mapping"><div class="titlepage"><div><div><h2 class="title" style="clear: both" id="jpa_overview_mapping_id">4. |
| Identity Mapping |
| </h2></div></div></div> |
| |
| <a class="indexterm" name="d5e5629"></a> |
| <a class="indexterm" name="d5e5631"></a> |
| <a class="indexterm" name="d5e5634"></a> |
| <p> |
| With our new knowledge of columns, we can map the identity fields of our |
| entities. The diagram below now includes primary key columns for our model's |
| tables. The primary key column for <code class="classname">Author</code> uses |
| nonstandard type <code class="literal"> INTEGER64</code>, and the <code class="literal">Magazine.isbn |
| </code> field is mapped to a <code class="literal">VARCHAR(9)</code> column instead of |
| a <code class="literal">VARCHAR(255)</code> column, which is the default for string |
| fields. We do not need to point out either one of these oddities to the JPA |
| implementation for runtime use. If, however, we want to use the JPA |
| implementation to create our tables for us, it needs to know about |
| any desired non-default column types. Therefore, the example following the |
| diagram includes this data in its encoding of our mappings. |
| </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-mapping-identity.png"></td></tr></table></div> |
| <p> |
| Note that many of our identity fields do not need to specify column information, |
| because they use the default column name and type. |
| </p> |
| <div class="example"><a name="jpa_overview_mapping_identityex"></a><p class="title"><b>Example 13.3. |
| Identity 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")) |
| public class Article { |
| |
| @Id 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 { |
| |
| @Column(name="AID", columnDefinition="INTEGER64") |
| @Id 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") |
| public class Contract |
| extends Document { |
| ... |
| } |
| |
| @Entity |
| @Table(name="SUB", schema="CNTRCT") |
| public class Subscription { |
| |
| @Id 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">Magazine</code> and <code class="literal">Company</code> |
| expressed in XML form: |
| </p> |
| <pre class="programlisting"> |
| <entity class="org.mag.Magazine"> |
| <id-class class="org.mag.Magazine.Magazine.MagazineId"/> |
| <table name="MAG"/> |
| <attributes> |
| <id name="isbn"> |
| <column length="9"/> |
| </id> |
| <id name="title"/> |
| ... |
| </attributes> |
| </entity> |
| <entity class="org.mag.pub.Company"> |
| <table name="COMP"/> |
| <attributes> |
| <id name="id"> |
| <column name="CID"/> |
| </id> |
| ... |
| </attributes> |
| </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_column.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_sequence.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">3. |
| Column |
| </td><td width="20%" align="center"><a accesskey="h" href="manual.html">Home</a></td><td width="40%" align="right" valign="top"> 5. |
| Generators |
| </td></tr></table></div></body></html> |