New Features
Enhanced Features
O/R Mapping
Tips and Tricks
UML Diagrams
VP Suite

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

  1. Create a class for being the ID Generator class.


  2. Right-click on the class, and select StereotypesStereotypes... from the popup menu


  3. Select ORM ID Generator from the left column and click > to add it as a stereotype of the ID Generator class.


  4. The class is converted into an ORM ID Generator Class. An operation is added automatically.


  5. Here is a sample Class Diagram.


  • Notice
    • If you have already implemented the generator class, draw the exact structure of package and class, and assign ORM ID Generator stereotype to your generator class will do the job.

Synchronize from Class Diagram to Entity Relationship Diagram

  1. 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.


  2. 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.


  3. 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.

  1. Select barcode in the Product entity. Right-click on your selection and select Open Specification... from the popup menu.


  2. In the Column Specification dialog box, select BarcodeGenerator from the ID Generator drop-down menu.


Animated Demo

Result

  • BarcodeGenerator.java
  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
  • Product.hbm.xml
  <?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

 
 
Last modified: 2006/08/12 15:18
 
 
Home | Recent Topics | Highlights | UML Diagrams | Tips and Tricks | Object-Relational Mapping
visual-paradigm.com Home | Training Center | UML Center | VP Gallery | Discussion Forum | UML Open Directory