| Primary Keys |
|
Primary Keys Primary Keys act as unique keys on a table. Primary keys are assumed not to be not null on the database. Primary keys (singletons) are assumed to be objects on the Beans. The JBean interface defines every bean to have the following primary key accessor methods: - public Object getPrimaryKey(); In addition every JBean written by JGenerator writes an accessor method for the primary key property. This is to ensure type safety for beans where the primary key is set by the user (externally set). For the example above the following accessor methods are written: - public Integer getAccountKey(); |
|
Properties Reader Primary Keys are specified as a <Null Modifier> on a column. For example: - Account.readTable.0=accountKey INT PRIMARY KEY Compound Keys are specified by having 2 PRIMARY KEY declarations. To switch off the primary keys on a table specify: - <ClassName>.primaryKey=false |
|
Jdbc Reader Reading primary keys can be controlled by the Jdbc driver. The behavior of the Jdbc Meta Data can be modified with the following properties: - jdbc.primaryKeyCatalog=<catalog> Once a table definition has been read in the primary key can be specified in the properties file. This is useful for tables where the primary key has not been set, the Jdbc driver isn't working,or for views where no primary key exists. The primary key is declared using the following syntax: - <ClassName>.<propertyName>.primaryKey=true For example: - Account.accountKey.primaryKey=true Compound Keys are specified by having 2 primaryKey declarations. |
|
Key Types Keys can be one of the following SQL types: -
In addition foreignKeys or Enumerated Types can be primary keys. ForeignKeys are used as primary keys in link tables. Enumerated Types are used as primary keys, when assigning a type to a secondary value. |
|
Key Generation There are 3 ways that Primary Keys can be generated: -
By default PrimaryKeys are generated internally. This means they are generated using a KeyTable on the database. To declare this default behavior across the database use the following syntax: - primaryKey.generation=internal Primary Keys can also be generated automatically. This means that they are generated automatically by the database. To declare this default behavior across the database use the following syntax: - primaryKey.generation=automatic Primary Keys can also be generated externally,then set on the Bean before creation. To declare this default behavior across the database use the following syntax: - primaryKey.generation=external This behavior can be overridden on a per table basis. <ClassName>.<propertyName>.generation=<GenerationType> For example: - UserProfile.<userCode>.generation=external The type of generation has a bearing on what the validateOnCreate method expects the primary key to be at create time. If the generation type is internal or automatic the validateOnCreate method expects the primary key to be null, and will throw an exception if not true. If the generation is external the validateOnCreate method will throw an exception if the primary key is null. |
|
Compound Keys A compound key is where two or more keys are used. The KeyClass writer will create a Compound Keys class for every class that has a compound key. Only one part of a primary key can be generated, automatically or internally. For example: - Account.readTable.0=majorKey INT PRIMARY KEY This will create the following example CompoundKey: - public class AccountKey implements java.io.Serializable public int getMajorKey(); public String getMinorKey(); |
|
Primary Key Methods The Table Bean has a number of methods to access primary keys. These are: -
The Column Bean has a number of methods to access primary keys. These are: -
|
|
Primary Key Suffix Primary Keys by default use the suffix Key, they can be changed to something else (e.g ID). This is specified as follows: PrimaryKey.suffix=<KEY> For example: PrimaryKey.suffix=ID
|