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.
|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 DataObjectUtils class.|
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:
The following custom methods should be added to the Painting class to access the values of ARTIST_ID and PAINTING_ID:
|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.|