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 produced 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 a generic and easy API to obtain an object primary key or to find an object from a known PK, skip to the next chapter on 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.|