In relational database, records are identified by a unique value, called the primary key. Some databases support controlling how this unique value can be generated, through the use of an ID generator. In this article you will learn how to select an ID generator for a primary key. And since ID generator is designed to serve primary key columns, it is only available to columns that are included in primary keys.
Setting ID generator for a column
ID generator is a column-specific option that can be set in the Column Specification window. To set ID generator:
- Right click on the primary key column of an entity and select Open Specification… from the popup menu.
- Select the ID generator from the drop-down menu of ID Generator. If you select sequence, native, seqhilo or hilo as ID Generator, you have to enter the key for the sequence/table name.
- Click OK to confirm editing.
Using sequence as ID generator
You can model a sequence and use it as the ID Generator. To model a sequence:
- Select Sequence from diagram toolbar.
- Click on the diagram to create a sequence shape.
- Enter its name.
- Specify the details of the sequence. Right click on the shape and select Open Specification… from the popup menu.
- Fill in the details of your sequence, such as Start With, Increment By, Min Value, Max Value, etc.
- Click OK to confirm editing.
- Now, you can select this sequence as an ID generator of primary key column.
Description of common ID generators
ID Generator | Description |
---|---|
assigned | allows the application to assign an identifier to the object before save() is called. |
guid | uses a database-generated GUID string on MS SQL Server and MySQL. |
hilo | uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a table and column as a source of hi values. The hi/lo algorithm generates identifiers that are unique only for a particular database. |
identity | supports identity columns in DB2, MySQL, MS SQL Server, Sybase and HypersonicSQL. The returned identifier is of type long, short or int. |
increment | generates identifiers of type long, short or int that are unique only when no other process is inserting data into the same table. Do not use in a cluster. |
native | (default) picks identity, sequence or hilo depending upon the capabilities of the underlying database. |
seqhilo | uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a named database sequence. |
sequence | uses a sequence in DB2, PostgreSQL, Oracle. The returned identifier is of type long,short or int |
Customizing ID generator
Besides the built-in strategies for generating ID, users can implement how ID will be generated by customizing an ID generator.
- In Class Diagram, create the ID generator class and stereotype it as ORM ID Generator.
- Right click on the primary key column that you want to select an ID generator for and select Open Specification… from the pop-up menu.
- In the Column Specification window, select the class in the ID Generator.
- Click OK to confirm.
- After generated the ORM code, look for the ID generator class and implement the generate method by returning an Integer or Long.
/** * Licensee: VP Development * License Type: Purchased */ import java.io.Serializable; import org.hibernate.engine.SessionImplementor; import org.hibernate.id.IdentifierGenerator; public class ProductIDGenerator implements IdentifierGenerator { public Serializable generate(SessionImplementor session, Object object) { //TODO: Implement Method throw new UnsupportedOperationException(); } } //ORM Hash:fae9faed19486e5f2b85c9d2d0d52cd9