A key generator provides the primary key values. A key generator, generates identity field values automatically. The db-key-generator defines the primary key generator for a class. The value attribute specifies the name of a built-in key generator. The first time a db-key-generator class is encountered in the metadata an instance is created and stored for later use.
OpenAccess ORM currently supports the following key generators:
1. |
|
HIGHLOW key generator – It is the default key generator and it uses a lookup table (the HIGHLOW or sequence block algorithm), i.e. it uses a last_used table to generate the id for a new instance. |
2. |
|
AUTOINC key generator – Using identity or auto increment columns. |
3. |
|
Verify – It checks whether the key field has been specified wherever required and it also verifies that one of the key fields have a non-default value during ObjectScope.Add. |
4. |
|
GUID – GUID as a key generator will be usable where new Guid values are required by the single field identity or multiple field identity. |
5. |
|
None – The application has to provide the key. |
A sample of the XML metadata is shown below:
|
Copy Code |
<class name="Branch">
<extension key="db-key-generator" value="AUTOINC" />
</class>
|
Properties for the HIGHLOW key generator
Property |
Default |
Description |
tableName |
voa_keygen |
Name of the keygen lookup table |
keyColumnName |
table_name |
Name of the primary key column |
valueColumnName |
last_used_id |
Name of the last used number column |
keyColumnLength |
64 |
Length of the primary key column |
grabSize |
10 |
The number of instances created per keygen table update. Using a grabSize > 1 improves performance but may result in "holes" in the sequence |
start |
0 |
If a table has no keygen row and start is 0 then the first primary key will be "select max (id) from table" + 1. Otherwise start is the first primary key |
createTable |
true |
If false the keygen table is left out of the generated schema |
pkConstraint |
pk_{tableName} |
Name of the primary key constraint for the keygen table |
OpenAccess ORM allows you to use a user-defined table as source for the HIGHLOW Key generator keys, for advanced tasks. In order to use this you need to change the default properties of the HIGHLOW Key generator. This could include simple things like changing the default grabSize value, in order to have less number of round-trips to the database for mass creating objects or changing the start value to fit into existing numbering, up to advanced task like defining you own table name, key column name and so on.
The changes to the default properties for the HIGHLOW key generator need to be made in the App.config file, in the Backend configurations section. Refer to Using the integrated HIGHLOW generator with a user defined table to know the exact changes needed to be made in the App.config file.