Custom ID generator for Object/Relational mapping
ID generator is for generating unique value for each row of record. You can assign an ID generator to a primary key column to indicate which strategy will be used when generating an ID in runtime. In addition to the default strategies, you can implement your own one.
Draw ORM ID Generator class
Create a class for being the ID Generator class.
Right-click on the class, and select
Stereotypes →
Stereotypes... from the popup menu
Select
ORM ID Generator from the left column and click
> to add it as a stereotype of the ID Generator class.
The class is converted into an
ORM ID Generator Class. An operation is added automatically.
Here is a sample Class Diagram.
Synchronize from Class Diagram to Entity Relationship Diagram
After completing the Class Diagram, it is the time to synchronize the Class Diagram to ERD. You can perform the synchronization in any of the two ways listed below.
Right-click on the Class Diagram, and select
Synchronize to Entity Relationship Diagram from the popup menu.
Alternatively, press the right-mouse button, move the mouse towards the right hand side to form a
left-right gesture indicated by the blue path shown on the Entity Relationship Diagram.
A
Sync to Entity Realtionship Diagram dialog box is appeared asking for the primary key of the
Product Table. Select
barcode as the primary key of this table.
Here is the Entity Relationship Diagram transformed from the Class Diagram.

Specifying ID Generator in column
After the ERD is generated, we are going to assign the ID Generator to the primary key column.
Select
barcode in the
Product entity. Right-click on your selection and select
Open Specification... from the popup menu.
In the
Column Specification dialog box, select
BarcodeGenerator from the
ID Generator drop-down menu.
Animated Demo
Result
import java.io.Serializable;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.id.IdentifierGenerator;
public class BarcodeGenerator implements IdentifierGenerator {
public Serializable generate(SessionImplementor session, Object object) {
//TODO: Implement Method
throw new UnsupportedOperationException();
}
}
//ORM Hash:0e1189665000b9b449648fdf7e688d68
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="inventory.Product" table="Product" lazy="false">
<id name="barcode" column="barcode" type="string">
<generator class="BarcodeGenerator">
</generator>
</id>
<property name="name" type="string" length="255" not-null="false" lazy="false"/>
<property name="description" type="string" length="255" not-null="false" lazy="false"/>
<property name="width" type="string" length="255" not-null="false" lazy="false"/>
<property name="height" type="string" length="255" not-null="false" lazy="false"/>
</class>
</hibernate-mapping>
Downloads
Resources