Composed keys
Composed candidate keys are often business keys. The underlying logic defines which objects are considered to be identical based on their values.
As an example, we consider a company having several departments. Regarding projects he following business rules shall apply:
-
Each department must have a unique name.
-
A project's name must be unique within the set of all projects belonging to the same department.
-
A project must be assigned to exactly one department.
Right now we defer considerations of the n:1 relationship between departments and projects to a later chapter. Instead we focus just on project instances and represent departments just by their integer id values which will later become foreign keys.
In addition each project receives a unique integer id value as well. This is in accordance with the “best practice” rule of defining a surrogate key ❷ to be used as (primary) object identifier. This immutable key will then become the target in foreign key definitions:
Java |
|
Sql |
|
Defining the surrogate primary key. |
|
Defining a business key composed of a project's
|
No. 18
JPA id
getter visibility.
Q: |
The setter void |
A: |
From an application developer's point of view the setter
should be absent: The When loading an instance from a database a persistence
provider however has to set its value. Hibernate uses the
reflection-API to override the restriction being imposed by
the So choosing |