| Apache Cayenne > Index > Cayenne FAQ > Mapping Primary Keys |
By default, Cayenne Modeler will not create a getter or setter for primary keys in the Java classes it generates. This is because primary keys are best viewed as a database artifact and generally aren't needed by the application.
Sometimes, however, a pre-existing schema will have meaningful primary keys (such as a Social Security Number) which must be visible or must be set directly. (It should be noted that Cayenne will not attempt to generate a primary key on INSERT unless the field is empty, therefore you can explicitly set a primary key value and bypass the generator.) To make a primary key available in a Java class, it must be mapped from the DbEntity to the ObjEntity, which is simple to do.
In Cayenne Modeler, do the following:
That's it. When you next generate your Java classes, a getter and setter will be created.
| Be Careful If you alter a record's primary key value and other records reference that value, you need to ensure those other records pick up the change to the key, too. |