hibernate tip: smart id generator

A small tip to generate more meaningful Id. Instead of 32 for OrderID use O000000032.

Use smart and meaningful ids

Use smart and meaningful ids

Simple implementations:

  • SmartIdTableGenerator
  • SmartIdSequenceGenerator

SmartIdTableGenerator

simple Table Generator Based SmartID generator. it extends TableGenerator and simply returns the formatted value. you can use format parameter for format option

public class SmartIdTableGenerator
extends org.hibernate.id.TableGenerator{

private DecimalFormat format;
/**read configuration parameters*/
@Override
public void configure(
Type type, Properties params, Dialect dialect) {
super.configure( type,  params,  dialect);

String formatPattern = params.getProperty("format");
if(formatPattern!=null)
format=new DecimalFormat(formatPattern);
}
/**get super value and format it*/
@Override
public synchronized Serializable generate(
SessionImplementor session, Object object)
throws HibernateException {
Serializable generated = super.generate(session, object);
String v=null;

if(generated instanceof Number){
if(format!=null)
v=format.format(generated);
else
v=String.valueOf(generated);
}else
v=(String) generated;

return v;
}
}

Usage:

@Entity
public class SimpleTableWithFormatPO {

@Id
@Column(name = "id", precision = 32)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SmartIdTableGenerator")
@GenericGenerator(name = "SmartIdTableGenerator", strategy = "com.altuure.smartid.SmartIdTableGenerator",
parameters = {@Parameter(name = "format", value = "LD0000000")})
private String id;

SmartIdSequenceGenerator

simple SequenceGenerator Based SmartID generator. it extends SequenceGenerator and simply returns the formatted value
use format parameter for format option

public class SmartIdSequenceGenerator extends org.hibernate.id.SequenceGenerator{

private DecimalFormat format;
public void configure(Type type, Properties params, Dialect dialect) {
super.configure( type,  params,  dialect);

String formatPattern = params.getProperty("format");
if(formatPattern!=null)
format=new DecimalFormat(formatPattern);
}
/**get super value and format it*/
@Override
public synchronized Serializable generate(SessionImplementor session, Object object)
throws HibernateException {
Serializable generated = super.generate(session, object);
String v=null;
// since the type of id is String hibernate returns Long.toString() Value
if(generated instanceof String)
generated=Long.parseLong((String)generated);
if(generated instanceof Number){
if(format!=null)
v=format.format(generated);
else
v=String.valueOf(generated);
}else
v=(String) generated;

return v;
}

}

Usage:

@Entity
public class SimpleSequenceWithFormatPO {

@Id
@Column(name = "id", precision = 32)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SmartIdSequenceGenerator")
@GenericGenerator(name = "SmartIdSequenceGenerator", strategy = "com.altuure.smartid.SmartIdSequenceGenerator",
parameters = {@Parameter(name = "format", value = "LD0000000")})
private String id;
//...
}


You can extend this implementations and add time variables,system variables or other variables as your data structure needs

eg:

  • LD200902000006
  • LDServerTitan00006

Sample Code
have fun :)

Related Posts

  1. hibernate native Id generator tip
  2. hibernate native id generator bug 3.2.4-ga
  3. Retrieving Id field from JPA and Hibernate
  4. Grails Days 1: GORM-CRUD
  5. hibernate criteria API bug: unnecessary fetch
  1. if use your original code
    IdentifierGeneratorFactory will throw
    new IdentifierGenerationException( “this id generator generates long, integer, short” )

    because in configure method, you call super.configure
    and you pass your identify return class (it’s String.class)

    so 
    you must change type when you call super.configure
    you can change like LongType, IntType…. 
    then it will be ok!!!

  1. No trackbacks yet.