How to choose the primary key ID Generator

by Ombretta Malinverno
7,186 views Published on Oct 24, 2011
Applies to: All versions
Table of contents

Modeling the domain information managed by the Web Application is a task that can be easily done with WebRatio. In fact, you have at your disposal many options that allow you to easily create the Domain Model starting from an existing database. Nevertheless you may need to manually add some new objects and to specify a Primary Key for each one. One of the most important aspects of the Primary Key is the way in which the Web Application creates the value for the Primary Key when creating a new object instance. When you work on the domain model, WebRatio automatically sets this Primary Key information for you. You may still want to customize this information; this article explains which options you can use and how to change them.

Since the way in which the primary key must be computed is related to the Web application business logic, it's easy to understand why this information is stored in the "Mapping" section of the primary key properties. In fact, the "Mapping" section contains information about the physical representation of the data. The property to consider is "ID Generator" which states which Hibernate ID Generator to use in order to create new values for the specific primary key.

To reach the "ID Generator" property:

  1. Select the Primary Key you want to change.
  2. Move to the Properties View.
  3. Select the "Mapping" Tab.

ID Generator property

Hibernate is the open source framework that the generated Web Application uses in order to communicate with the database. WebRatio currently includes Hibernate version 3.6. If you look at the official Hibernate documentation you can find the list of possible ID Generator values. WebRatio lets you use a subset of the available options.

When you create an object having an integer or float Attribute as Primary Key, the default ID Generator option is "Increment (select max +1)". This option means that before adding the new object, the Web Application retrieves the maximum value stored for that attribute and adds "1" in order to get the new value.

The "Increment (select max+1)" ID Generator must not be used when you are going to deploy your Web Application in a cluster.

An alternative can be to use the "Identity" or the "Sequence" ID Generator:

  • "Identity" refers to the database that retrieves the correct value for the Primary Key. This option is valid only for databases in which you can define "Identity" columns, such as DB2, MySQL, and SQL Server.
  • "Sequence" means that the database retrieves the Primary Key value using a sequence specified on the database itself. This option can be used only on databases supporting sequences, such as DB2, PostgreSQL, and Oracle. Once you choose this option, you must also select which sequence to be used, selecting it in the "Sequence Name" property. Remember that in order to see the sequence list you must refresh the database connection from WebRatio.

When you create an object having a string Attribute as the Primary Key, the default ID Generator option is "Assigned". This option means that the WebRatio modeler has to bind correctly the input parameters needed in order to create new object instances, because the Web Application cannot automatically create a value for the Primary Key. The "Assigned" option is also used when working with composed primary keys

Alternatives to the "Assigned" ID Generator are:

  • "guid", which uses a database-generated GUID string. This option works only for MS SQL Server and MySQL.
  • "uuid", which uses a128-bit UUID algorithm to generate identifiers of the type strings that are unique within a network (the IP address is used). The UUID is encoded as a string that is 32 hexadecimal digits in length. The "uuid" ID Generator can be used only when the databases support the options.

WebRatio automatically filters the ID Generator list showing only the applicable option depending on the Primary Key type and composition, and also considering the used database engine. 

 
 

Related Learning Objects