cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Cayenne Documentation > Primary Key Generation
Date Fri, 31 Jul 2009 08:16:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CAYDOC&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/CAYDOC/Primary+Key+Generation">Primary
Key Generation</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~ari">Ari
Maniatis</a>
    </h4>
     trying again to get the picture to stick
          <div id="versionComment" class="noteMacro" style="display:none; padding: 5px;">
     trying again to get the picture to stick<br />
     </div>
          <br/>
     <div class="notificationGreySide">
         <h2><a name="PrimaryKeyGeneration-PrimaryKeyGeneration"></a>Primary
Key Generation</h2>
<p>Depending on the type of persistent objects and the database design, primary key
values can be handled in a few different ways. A decision to use one or another technique
will affect how the mapping is done. Here is a summary of possible scenarios. Each one is
described below in details.</p>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>&nbsp;</th>
<th class='confluenceTh'>PK Has DbAttribute</th>
<th class='confluenceTh'>PK Has ObjAttribute</th>
<th class='confluenceTh'>PK is a DataObject property</th>
</tr>
<tr>
<td class='confluenceTd'>Meaningful Primary Key</td>
<td class='confluenceTd'>Yes</td>
<td class='confluenceTd'>Yes</td>
<td class='confluenceTd'>Yes</td>
</tr>
<tr>
<td class='confluenceTd'>Primary Key Derived From Relationship</td>
<td class='confluenceTd'>Yes</td>
<td class='confluenceTd'>No</td>
<td class='confluenceTd'>No</td>
</tr>
<tr>
<td class='confluenceTd'>Database-Generated Primary Key</td>
<td class='confluenceTd'>Yes</td>
<td class='confluenceTd'>No</td>
<td class='confluenceTd'>No</td>
</tr>
<tr>
<td class='confluenceTd'>Cayenne-Generated Primary Key</td>
<td class='confluenceTd'>Yes</td>
<td class='confluenceTd'>No</td>
<td class='confluenceTd'>No</td>
</tr>
</tbody></table>

<h3><a name="PrimaryKeyGeneration-MeaningfulPrimaryKey"></a>Meaningful Primary
Key</h3>

<p>DataObjects can provide their own values for the primary key columns. Sometimes this
makes sense, for example if a primary key column contains meaningful data like a Social Security
Number. In such cases primary key handling is no different from handling any other persistent
attributes. Each primary key column (DbAttribute) will have an ObjAttribute mapped to it.
The Java class implementing DataObject will contain a property described by this ObjAttribute.
The value of this property will be saved to the database.</p>

<h3><a name="PrimaryKeyGeneration-PrimaryKeyDerivedFromRelationship"></a>Primary
Key Derived From Relationship</h3>

<p>A primary key column of a table may depend on a primary key column of another table.
This is normally the case with "join" tables used to resolve many-to-many relationships. In
such cases DataObject normally does not contain a property mapped to a PK column. Instead
the value is automatically derived by Cayenne from a primary key of a related object using
a relationship.</p>

<h3><a name="PrimaryKeyGeneration-PrimaryKeyProvidedbyDatabaseonINSERT"></a>Primary
Key Provided by Database on INSERT</h3>

<p>Sometimes databases have their own proprietary mechanism to generate a primary key
when a new row is inserted. This feature is called "auto increment" or "identity column".
Cayenne supports such primary keys out of the box. For this mechanism to be invoked, the DbEntity
primary key generation must be marked as "Database-Generated" in the Modeler. Currently MySQL
and SQLServer adapters have this feature turned on by default.<br/>
<img src="/confluence/download/attachments/10571/Picture 1.png" align="absmiddle" border="0"
/></p>

<h3><a name="PrimaryKeyGeneration-CayenneGeneratedPrimaryKey"></a>Cayenne
Generated Primary Key</h3>

<p>In most cases, the database primary key is a purely relational concept that has no
correspondence in the object model. Normally this is a unique sequential number that identifies
a row in the database. This means that creating a property in a persistent object that corresponds
to a primary key is an artificial step. To avoid doing that Cayenne assigns each object an
ObjectId that internally handles the details of primary key handling.</p>

<p>When a new DataObject is saved, and its properties and relationships do not contain
primary key values, Cayenne will attempt to automatically generate a key value. Generation
mechanism depends on the DbAdapter used and can be customized by users by subclassing one
of the included adapters. Adapters shipped with Cayenne use the following default PK generation
strategies:</p>

<ul>
	<li>MySQL, PostgreSQL, HSQL Adapters: use select/update from a special AUTO_PK_TABLE
lookup table.</li>
	<li>Oracle Adapter: uses Oracle sequences.</li>
	<li>DB2 Adapter: uses DB2 sequences.</li>
	<li>SQLServer , Sybase Adapters: use a strored procedure tied to AUTO_PK_TABLE lookup
table.</li>
	<li>OpenBase Adapter: uses OpenBase specific NEWID mechanism.</li>
</ul>


<p>To generate the necessary database objects for the primary key autogeneration, use
CayenneModeler and follow the instructions given <a href="/confluence/display/CAYDOC/Generate+Primary+Key+Support"
title="Generate Primary Key Support">here</a>.</p>

<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td>Cayenne only supports
automatic PK generation for a single column per table.</td></tr></table></div>

<h3><a name="PrimaryKeyGeneration-Sections"></a>Sections</h3>

<ol>
	<li><a href="/confluence/display/CAYDOC/Generated+Columns" title="Generated Columns">Generated
Columns</a></li>
</ol>

     </div>
     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/CAYDOC/Primary+Key+Generation">View
Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=10571&revisedVersion=5&originalVersion=4">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/CAYDOC/Primary+Key+Generation?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message