RE: Primary Key Access

From: Dave Merrin (dmerri..pasystems.co.uk)
Date: Wed Oct 26 2005 - 09:26:35 EDT

  • Next message: Andrus Adamchik: "Re: Beginners Problem"

    Hi Mike,

    thanks for the info. I read up on velocity and had a look through the
    ClassGenerator code. I've managed to produce a template which does a little
    string manipulation to sort the name out.

    I have a table with a composite key. The composite key is made up from the
    primary keys of two other tables. I thought I was going to have to retrieve
    the other objects to get their primary keys. From testing it works out that
    I don't need to do that. Everything is working just as I want it.

    thanks again,

    Dave

    > -----Original Message-----
    > From: Mike Kienenberger [mailto:mkienen..mail.com]
    > Sent: 25 October 2005 16:42
    > To: cayenne-use..bjectstyle.org
    > Subject: Re: Primary Key Access
    >
    >
    > > getItem_id()
    >
    > The reason for this is that you're outputting DbAttributes rather than
    > ObjAttributes, and your DbAttribute is named "item_id". I can't
    > think of an easy way to deal with this under cgen v1.1. You'll have
    > to write velocity code to perform the transformation.
    >
    > I don't understand your second issue.
    > classGen.Entity.DbEntity.PrimaryKey should only return the primary
    > keys for that entity, so it's unclear to me why you'd be seeing
    > foreign keys unless you've mistakenly marked them as primary keys in
    > your model.
    >
    >
    > On 10/25/05, Dave Merrin <dmerri..pasystems.co.uk> wrote:
    > > Hi,
    > >
    > > sorry to bug you with another question. All your help has been great.
    > >
    > > I'm trying to modify the template for the class generation. I
    > want to give
    > > access to primary keys. I've added the following code to the
    > template. It
    > > works apart from getting a weird method name e.g. getItem_id().
    > Does anybody
    > > know how to sort this so I get something like getItemId()?
    > >
    > > #if( $classGen.Entity.DbEntity )
    > > #foreach( $idAttr in ${classGen.Entity.DbEntity.PrimaryKey} )
    > > #set( $classGen.Prop = $idAttr.Name )## let controller know
    > about current
    > > property
    > > /** Read-only access to PK */
    > > public Integer get${classGen.cappedProp}() {
    > > return (getObjectId() != null && !getObjectId().isTemporary())
    > > ?
    > >
    > (Integer)getObjectId().getIdSnapshot().get(${classGen.propAsConsta
    > ntName}_PK
    > > _COLUMN)
    > > : null;
    > > }
    > > #end
    > > #end
    > >
    > > Unfortunately the same code is generated for foreign keys. I
    > see from the
    > > documentation I should have something more like below as a result. This
    > > accesses the Artist to get the primary key from the artist.
    > Does this mean
    > > another query is made to get the artist? If so is there any way
    > to get the
    > > id's without accessing the artist? Also how do I differentiate in the
    > > template between a primary key and a foreign key?
    > >
    > > /** Read-only access to FK */
    > > public Integer getArtistId() {
    > > Artist artist = getArtist();
    > > return (artist != null)
    > > ?
    > >
    > (Integer)artist.getObjectId().getIdSnapshot().get(Artist.ARTIST_ID
    > _PK_COLUMN
    > > )
    > > : null;
    > > }
    > >
    > > Thanks again for all the help on the previous issues.
    > >
    > > Dave
    > >
    > >
    >



    This archive was generated by hypermail 2.0.0 : Wed Oct 26 2005 - 09:30:56 EDT