Using ID Generator
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.
Selecting ID generator - 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.
Select Sequence - Click on the diagram to create a sequence shape.
- Enter its name.
Sequence created - 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.
Sequence Specification window - Click OK to confirm editing.
- Now, you can select this sequence as an ID generator of primary key column.
Selecting sequence as ID generator
Description of common ID generators
|
||||||||||||||||||
Description of common ID generators |
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.
An ID generator class - 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.
Click Open Specification... from the pop-up menu - In the Column Specification window, select the class in the ID Generator.
Select an ID generator in Column Specification window - 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
Related Resources
The following resources may help you to learn more about the topic discussed in this page.
11. Working with Unique Constraint | Table of Contents | 13. Different Inheritance Strategies |