Re: Primary Key Access

From: Mike Kienenberger (mkienen..mail.com)
Date: Wed Oct 26 2005 - 10:40:40 EDT

  • Next message: Lindsay Steele: "Re: Beginners Problem"

    Dave,

    If you are doing a lot of fancy stuff with cgen, I'd recommend you
    consider using the Cayenne 1.2 cgen with version=1.2. This update
    greatly improved the class generation, and has hooks to allow you to
    expand cgen as much as you need. For instance, you can add another
    velocity tool (POJO class) to the context to convert your names using
    java.

    Remember that you can always use a different version of cayenne for
    running cgen than you use in your applications.

    -Mike

    On 10/26/05, Dave Merrin <dmerri..pasystems.co.uk> wrote:
    > 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 - 10:40:42 EDT