blob: 69ef17efeb4b912331344d4e92669e0bfe0e7c0b [file] [log] [blame]
Title: Accessing PK and FK Values
<P>Normally it is not advisable to map primary and foreign key columns (PK and FK) as Java class properties (ObjAttributes). When reverse engineering of the database is done using CayenneModeler, the generated mapping will reflect that PKs and FKs will not be included in the Java class. However, sometimes an application requires access to these values.</P>
<DIV class="panelMacro"><TABLE class="infoMacro"><COLGROUP><COL width="24"><COL></COLGROUP><TR><TD valign="top"><IMG src="http://cayenne.apache.org/docs/3.0/images/icons/emoticons/information.gif" width="16" height="16" align="absmiddle" alt="" border="0"></TD><TD>For an easier way to obtain an object primary key or to find an object from a known PK, skip to the chapter on the <A href="dataobjectutils.html" title="DataObjectUtils">DataObjectUtils</A> class.</TD></TR></TABLE></DIV>
<P>Cayenne provides a generic way to access primary and foreign keys by creating custom get* methods in the DataObject subclass. Lets take a Painting class as an example. The following code is generated by CayenneModeler:</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
<SPAN class="code-keyword">package</SPAN> org.apache.art;
<SPAN class="code-keyword">public</SPAN> class Painting <SPAN class="code-keyword">extends</SPAN> org.apache.art.auto._Painting {
}
</PRE>
</DIV></DIV>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
<SPAN class="code-keyword">package</SPAN> org.apache.art.auto;
/** <SPAN class="code-object">Class</SPAN> _Painting was generated by Cayenne.
* It is probably a good idea to avoid changing <SPAN class="code-keyword">this</SPAN> class manually,
* since it may be overwritten next time code is regenerated.
* If you need to make any customizations, please use subclass.
*/
<SPAN class="code-keyword">public</SPAN> class _Painting <SPAN class="code-keyword">extends</SPAN> org.apache.art.ArtDataObject {
<SPAN class="code-keyword">public</SPAN> <SPAN class="code-keyword">static</SPAN> <SPAN class="code-keyword">final</SPAN> <SPAN class="code-object">String</SPAN> ESTIMATED_PRICE_PROPERTY = <SPAN class="code-quote">&quot;estimatedPrice&quot;</SPAN>;
<SPAN class="code-keyword">public</SPAN> <SPAN class="code-keyword">static</SPAN> <SPAN class="code-keyword">final</SPAN> <SPAN class="code-object">String</SPAN> PAINTING_TITLE_PROPERTY = <SPAN class="code-quote">&quot;paintingTitle&quot;</SPAN>;
<SPAN class="code-keyword">public</SPAN> <SPAN class="code-keyword">static</SPAN> <SPAN class="code-keyword">final</SPAN> <SPAN class="code-object">String</SPAN> TO_ARTIST_PROPERTY = <SPAN class="code-quote">&quot;toArtist&quot;</SPAN>;
<SPAN class="code-keyword">public</SPAN> <SPAN class="code-keyword">static</SPAN> <SPAN class="code-keyword">final</SPAN> <SPAN class="code-object">String</SPAN> TO_GALLERY_PROPERTY = <SPAN class="code-quote">&quot;toGallery&quot;</SPAN>;
<SPAN class="code-keyword">public</SPAN> <SPAN class="code-keyword">static</SPAN> <SPAN class="code-keyword">final</SPAN> <SPAN class="code-object">String</SPAN> TO_PAINTING_INFO_PROPERTY = <SPAN class="code-quote">&quot;toPaintingInfo&quot;</SPAN>;
<SPAN class="code-keyword">public</SPAN> <SPAN class="code-keyword">static</SPAN> <SPAN class="code-keyword">final</SPAN> <SPAN class="code-object">String</SPAN> PAINTING_ID_PK_COLUMN = <SPAN class="code-quote">&quot;PAINTING_ID&quot;</SPAN>;
<SPAN class="code-keyword">public</SPAN> void setEstimatedPrice(java.math.BigDecimal estimatedPrice) {
writeProperty(<SPAN class="code-quote">&quot;estimatedPrice&quot;</SPAN>, estimatedPrice);
}
<SPAN class="code-keyword">public</SPAN> java.math.BigDecimal getEstimatedPrice() {
<SPAN class="code-keyword">return</SPAN> (java.math.BigDecimal)readProperty(<SPAN class="code-quote">&quot;estimatedPrice&quot;</SPAN>);
}
<SPAN class="code-keyword">public</SPAN> void setPaintingTitle(<SPAN class="code-object">String</SPAN> paintingTitle) {
writeProperty(<SPAN class="code-quote">&quot;paintingTitle&quot;</SPAN>, paintingTitle);
}
<SPAN class="code-keyword">public</SPAN> <SPAN class="code-object">String</SPAN> getPaintingTitle() {
<SPAN class="code-keyword">return</SPAN> (<SPAN class="code-object">String</SPAN>)readProperty(<SPAN class="code-quote">&quot;paintingTitle&quot;</SPAN>);
}
...
}
</PRE>
</DIV></DIV>
<P>The following custom methods should be added to the Painting class to access the values of ARTIST_ID and PAINTING_ID:</P>
<DIV class="panelMacro"><TABLE class="infoMacro"><COLGROUP><COL width="24"><COL></COLGROUP><TR><TD valign="top"><IMG src="http://cayenne.apache.org/docs/3.0/images/icons/emoticons/information.gif" width="16" height="16" align="absmiddle" alt="" border="0"></TD><TD>If you perform class generation using Ant, you can customize class generation templates to generate these methods for you. Eventually CayenneModeler will support this too as optional functionality.</TD></TR></TABLE></DIV>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
<SPAN class="code-keyword">package</SPAN> org.apache.art;
<SPAN class="code-keyword">public</SPAN> class Painting <SPAN class="code-keyword">extends</SPAN> org.apache.art.auto._Painting {
/** Read-only access to PK */
<SPAN class="code-keyword">public</SPAN> <SPAN class="code-object">Integer</SPAN> getPaintingId() {
<SPAN class="code-keyword">return</SPAN> (getObjectId() != <SPAN class="code-keyword">null</SPAN> &amp;&amp; !getObjectId().isTemporary())
? (<SPAN class="code-object">Integer</SPAN>)getObjectId().getIdSnapshot().get(PAINTING_ID_PK_COLUMN)
: <SPAN class="code-keyword">null</SPAN>;
}
/** Read-only access to FK */
<SPAN class="code-keyword">public</SPAN> <SPAN class="code-object">Integer</SPAN> getArtistId() {
Artist artist = getArtist();
<SPAN class="code-keyword">return</SPAN> (artist != <SPAN class="code-keyword">null</SPAN>)
? (<SPAN class="code-object">Integer</SPAN>)artist.getObjectId().getIdSnapshot().get(Artist.ARTIST_ID_PK_COLUMN)
: <SPAN class="code-keyword">null</SPAN>;
}
}
</PRE>
</DIV></DIV>