Re: Accessing PK and FK values of DataObjects

From: James Treleaven (jametre..noreo.on.ca)
Date: Tue Dec 09 2003 - 13:05:23 EST

  • Next message: Andrus Adamchik: "Re: Accessing PK and FK values of DataObjects"

    Thank you for your very clear explanations, Andrus.

    Might I ask how one searches on PK and FK values? (I cannot specify
    these columns in expressions I pass to SelectQuery(), can I?)

    I expect the answer is 'Use Raw SQL' - but I am far from expert when it
    comes to Cayenne.

    thanks,
    James

    Andrus Adamchik wrote:
    > This is probably the most frequently asked question on this list. So I
    > figured it is time to write the docs for it. Here is an unformatted
    > version of this page. It will be included in 1.0.4 release docs.
    >
    > Andrus
    >
    >
    >
    > 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, produced
    > mapping will reflect that - PKs and FKs will not be included in the
    > Java class. However sometimes the application requires access to these
    > values. Cayenne provides an easy way to do that by creating custom get*
    > methods in the DataObject subclass. Lets take a Painting class as an
    > example. The following code is generated by CayenneModeler:
    >
    > package org.objectstyle.art;
    >
    > public class Painting extends org.objectstyle.art.auto._Painting {
    >
    > }
    >
    > package org.objectstyle.art.auto;
    >
    > /** Class _Painting was generated by Cayenne.
    > * It is probably a good idea to avoid changing this class manually,
    > * since it may be overwritten next time code is regenerated.
    > * If you need to make any customizations, please use subclass.
    > */
    > public class _Painting extends org.objectstyle.art.ArtDataObject {
    >
    > public static final String ESTIMATED_PRICE_PROPERTY =
    > "estimatedPrice";
    > public static final String PAINTING_TITLE_PROPERTY = "paintingTitle";
    > public static final String TO_ARTIST_PROPERTY = "toArtist";
    > public static final String TO_GALLERY_PROPERTY = "toGallery";
    > public static final String TO_PAINTING_INFO_PROPERTY =
    > "toPaintingInfo";
    >
    > public static final String PAINTING_ID_PK_COLUMN = "PAINTING_ID";
    >
    > public void setEstimatedPrice(java.math.BigDecimal estimatedPrice) {
    > writeProperty("estimatedPrice", estimatedPrice);
    > }
    > public java.math.BigDecimal getEstimatedPrice() {
    > return (java.math.BigDecimal)readProperty("estimatedPrice");
    > }
    >
    >
    > public void setPaintingTitle(String paintingTitle) {
    > writeProperty("paintingTitle", paintingTitle);
    > }
    > public String getPaintingTitle() {
    > return (String)readProperty("paintingTitle");
    > }
    > ...
    > }
    >
    > The following custom methods should be added to the Painting class to
    > access the value of ARTIST_ID and PAINTING_ID:
    > Note:
    > If you perform class generation using Ant, you can customize class
    > generation templates to generte these methods for you. Eventually
    > CayenneModeler will support this too as optional functionality.
    >
    > package org.objectstyle.art;
    >
    > public class Painting extends org.objectstyle.art.auto._Painting {
    > /** Read-only access to PK */
    > public Integer getPaintingId() {
    > return (getObjectId() != null && !getObjectId().isTemporary())
    > ?
    > (Integer)getObjectId().getIdSnapshot().get(PAINTING_ID_PK_COLUMN)
    > : null;
    > }
    >
    > /** Read-only access to FK */
    > public Integer getArtistId() {
    > Artist artist = getArtist();
    > return (artist != null)
    > ?
    > (Integer)artist.getObjectId().getIdSnapshot().get(Artist.ARTIST_ID_PK_CO
    > LUMN)
    > : null;
    > }
    > }
    >
    >



    This archive was generated by hypermail 2.0.0 : Tue Dec 09 2003 - 13:05:31 EST