You may also want to take a look at DataObjectUtils:
http://objectstyle.org/cayenne/api/cayenne/org/objectstyle/cayenne/
DataObjectUtils.html
As for accessing FK, how about turning it around - FK is a PK of
another object, so you may do something like this:
DataObject relatedObject = this.getXYZ();
int fk = relatedObject != null ? DataObjectUtils.intPKForObject
(relatedObject) : -1;
Andrus
On Oct 25, 2005, at 11:29 AM, Dave Merrin 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.propAsConstantName}_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 : Tue Oct 25 2005 - 11:57:46 EDT